krotbutler.blogg.se

Python mac address formatting
Python mac address formatting




python mac address formatting

Mac2=anslate(str.maketrans('', '', '-:_')) #strips normal chars HexChars = ĭef CheckMac(mac1): #returns false if valid This is a crude way to work out the manufacturer of a NIC card and might be a useful clue if providing info to a user.

python mac address formatting

It then prints the OUI details if an entry in the database is found. The main function (printMacs) validates the input string and then prints each of the formats, including the two custom ones. This might help in rare cases to ensure the input is truly correct rather than mathematically correct. The CheckMac function in my code checks that the number of characters is 12 and that there are no non-hex characters. The netaddr library doesn’t validate for regular functions such as when you change format and so you could in theory enter a MAC address “0123” at it be interpreted as “00-00-00-00-00-7B” For example it would validate as true a string of 11 characters (e.g. There is a validation in the netaddr library through the valid_mac function which does validate to an extent but with results that I didn’t like. You can easily imagine, given a MAC address in a variable, that the CLI command needed for both a cisco and HP switch could be created.

python mac address formatting

The input here is via a prompt to demonstrate the code but this could easily be a variable. The code below uses it together with some validation to show how you can get a different format of MAC address from an input. The python library netaddr manipulates IP and MAC addresses. If you run HPE/Aruba switches the discussion on custom formats will be useful.

Python mac address formatting how to#

This post shows to check the string is a valid address and how to convert between formats. Every vendor has their favourite format for MAC addresses and it is common to need to create strings to pass as CLI commands.






Python mac address formatting