Search code examples
pythonarp

How do I get ip address and mac address from arp output via Python regex?


I would like to get mac addresses and corresponding ip addresses (e.g. as a dictionary with mac address as keys and ip addresses) as value from arp command line output on Ubuntu.

$ arp
Address                  HWtype  HWaddress           Flags Mask            Iface
XX.X.X.X(XX)                ether   XX:XX:XX:XX:XX:XX   C                     eth0
<host name>                 ether   XX:XX:XX:XX:XX:XX   C                     eth0

The regex for ip addresses (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) and the regex for macs (?P<mac>[0-9a-f]{2}[:]){5}([0-9a-f]{2}) each for it alone works fine. How can I combine those two and ignore everything between ip addresses and macs? How could I improve the ip address regex to match host names as well? In case there is a better alternative to using arp to get both, ip address and macs I am happy to use this command instead.


Solution

  • For joining them and ignore the intermediate information, just add the ".*" in the middle, so it matches anything, but is won't be assigned to a group:

    (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*([0-9a-f]{2}[:]){5}([0-9a-f]{2})
    

    To account for hostnames, it will probably be better to caputure the first field without trying to make sure it is a proper IP or hostname. Just capture the first field, until the space:

    ^(?P<ip>[^\s]+)
    

    So the first field with the hole and the MAC ends up being:

    ^(?P<ip>[^\s]+).*(?P<mac>(?:[0-9a-f]{2}:){5}[0-9a-f]{2})