Python gets a list of mac addresses and compares them with a list from a file

I am starting in Python and hope you can help me solve this problem.

I have a list of mac addresses written in a file. I need to get a list of mac addresses on the network, compare them with the addresses from the file, and then type in stdout the addresses from the file that are not found in the address list from the network.

And at the end, update the file with these addresses.

Now I managed to read the file that I give as an argument:

import sys

with open(sys.argv[1], 'r') as my_file:
   lines = my_file.read()

my_list = lines.splitlines()

I am trying to read mac addresses by running process arp from python:

import subprocess

addresses = subprocess.check_output(['arp', '-a'])

But with this code, I get the following:

  Internet Address      Physical Address      Type
  156.178.1.1           5h-c9-6f-78-g9-91     dynamic
  156.178.1.255         ff-ff-ff-ff-ff-ff     static
  167.0.0.11            05-00-9b-00-00-10     static
  167.0.0.123           05-00-9b-00-00-ad     static
  .....

How can I filter here to get only mac address list?

Or I can check two lists, like this, to find out if there is a MAC address from a file on the network and not print it?

+4
4

, :

networkAdds = addresses.splitlines()[1:]
networkAdds = set(add.split(None,2)[1] for add in networkAdds if add.strip())
with open(sys.argv[1]) as infile: knownAdds = set(line.strip() for line in infile)

print("These addresses were in the file, but not on the network")
for add in knownAdds - networkAdds:
    print(add)
+2

split() count - :

text = """  Internet Address      Physical Address      Type
  156.178.1.1           5h-c9-6f-78-g9-91     dynamic
  156.178.1.255         ff-ff-ff-ff-ff-ff     static
  167.0.0.11            05-00-9b-00-00-10     static
  167.0.0.123           05-00-9b-00-00-ad     static
  ....."""

for item in text.split():
    if item.count('-') == 5:
        print item

:

5h-c9-6f-78-g9-91
ff-ff-ff-ff-ff-ff
05-00-9b-00-00-10
05-00-9b-00-00-ad

(listcomps):

print [item for item in text.split() if item.count('-') == 5]

:

['5h-c9-6f-78-g9-91', 'ff-ff-ff-ff-ff-ff', '05-00-9b-00-00-10', '05-00-9b-00-00-ad']
0

/proc/net/arp :

with open("/proc/net/arp") as f, open(sys.argv[1], 'r') as my_file:
    next(f)
    mac_addrs = {mac for  _, _, _, mac,_, _ in map(str.split, f)}
    for mac in map(str.rstrip, my_file):
        if mac not in mac_addrs:
           print("No entry for addr: {}".format(mac))

/proc/net/arp :

IP address       HW type     Flags       HW address            Mask     Device
xxx.xxx.xxx.xxx    0x1         0x2         xx:xx:xx:xx:xx:xx     *        wlan0

- mac/hw. arp, arp -an, .

mac, , , "a+" , :

with open("/proc/net/arp") as f, open(sys.argv[1], 'a+') as my_file:
        next(f)
        mac_addrs = {mac for _, _, _, mac,_, _ in map(str.split, f)}
        for mac in map(str.rstrip, my_file):
            if mac not in mac_addrs:
               print("No entry for addr: {}".format(mac))
            else:
                mac_addrs.remove(mac)
        my_file.writelines("{}\n".format(mac)for mac in mac_addrs)
0

MAC-, :

import re

addresses = """  Internet Address      Physical Address      Type
156.178.1.1           5f-c9-6f-78-f9-91     dynamic
156.178.1.255         ff-ff-ff-ff-ff-ff     static
167.0.0.11            05-00-9b-00-00-10     static
167.0.0.123           05-00-9b-00-00-ad     static
....."""

print(re.findall(('(?:[0-9a-fA-F]{1,}(?:\-|\:)){5}[0-9a-fA-F]{1,}'),addresses))

:

['5f-c9-6f-78-f9-91', 'ff-ff-ff-ff-ff-ff', '05-00-9b-00-00-10', '05-00-9b-00-00-ad']

As far as I can see, your MAC addresses do not match the naming conventions used in this regex, so you need to play with the tag [0-9a-fA-F].

0
source

Source: https://habr.com/ru/post/1616274/


All Articles