burim:python:python-labs:loesung_dateien_einlesen3.py
FNAME = "ifconfig_u18.txt"  # Ubuntu 18.04
# FNAME = "ifconfig.txt"  # Ubuntu 16.04
ADAPTERS = dict()
SEARCHSTR = "ether"  # Ubuntu 18.04
# SEARCHSTR = "HWaddr"  # Ubuntu 16.04
MACADDRLEN = 17  # 6 Byte = 6 * 2 = 12 + 5 = 17
macaddr = ""

with open(FNAME) as fid:
    for linenumber, line in enumerate(fid):
        if (line and ("lo" not in line) and ("tun" not in line)):
            if line[0] != " ":
                adapter = line.split(" ")[0].replace(":", "")
            if SEARCHSTR in line:
                startidx = line.find(SEARCHSTR) + len(SEARCHSTR) + 1
                macaddr = line[startidx : startidx + MACADDRLEN]
            if adapter.strip() and macaddr:
                ADAPTERS[adapter] = macaddr


FNAME_BASE = FNAME[:FNAME.rfind(".txt")]
FNAME_OUT = FNAME_BASE + "_adapters.txt"
FNAME_OUT2 = FNAME_BASE + "_adapters_changed.txt"

with open(FNAME_OUT, "w") as fid:
    for linenumber, (adapter, mac) in enumerate(ADAPTERS.items()):
        print(linenumber, adapter, mac)
        fid.write("Adapter: " +  adapter + ", MAC: " + mac + "\n")

BASEMAC = "01:23:45:ff:00:"
with open(FNAME_OUT2, "w") as fid:
    for i, (adapter, mac) in enumerate(ADAPTERS.items()):
        mac_changed = BASEMAC + "%02x" %(i+1)
        mac_changed = BASEMAC + "{0:02x}".format((i+1))
        fid.write("Adapter: " +  adapter + ", MAC: " + mac_changed + "\n")
burim/python/python-labs/loesung_dateien_einlesen3.py.txt · Last modified: 2019/01/21 20:55 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki