Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python获取mac地址列表,并将其与文件中的列表进行比较_Python_Mac Address - Fatal编程技术网

Python获取mac地址列表,并将其与文件中的列表进行比较

Python获取mac地址列表,并将其与文件中的列表进行比较,python,mac-address,Python,Mac Address,我是Python的初学者,希望您能帮助我解决这个问题 我有一个写在文件中的mac地址列表。我需要获取网络上的mac地址列表,将它们与文件中的地址进行比较,然后在标准输出中打印在网络地址列表中找不到的文件中的地址 最后用这些地址更新文件 现在,我设法读取了一个作为参数给出的文件: import sys with open(sys.argv[1], 'r') as my_file: lines = my_file.read() my_list = lines.splitlines() 我

我是Python的初学者,希望您能帮助我解决这个问题

我有一个写在文件中的mac地址列表。我需要获取网络上的mac地址列表,将它们与文件中的地址进行比较,然后在标准输出中打印在网络地址列表中找不到的文件中的地址

最后用这些地址更新文件

现在,我设法读取了一个作为参数给出的文件:

import sys

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

my_list = lines.splitlines()
我试图通过从python运行进程arp来读取mac地址:

import subprocess

addresses = subprocess.check_output(['arp', '-a'])
但通过这段代码,我得到了:

  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
  .....
我如何在这里进行筛选,以便仅获取mac地址列表

或者我可以像这样检查两个列表,看看文件中的mac地址是否在网络上,如果不打印出来?

使用split()统计每个项目中
-
字符的数量:

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
['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):

输出:

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

从你所拥有的开始:

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)

您可以解析
/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)

要获取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']

据我所知,您的MAC地址没有遵循此regexp中使用的命名约定,因此您可以使用
[0-9a-fA-F]
这件事。

我在line networkAdds=set(add.split(None,2)[1]处得到一个索引器,用于加载项networkAdds):索引超出范围。@scm:确保子流程的输出中没有空行。那么,这是计算机设置吗?因为我在另一台没有空行的计算机上测试了它,它可以工作。我怎样才能确保没有空行?@schmimona:查看更新。这将修复空行问题,并最终用这些地址更新文件。更新哪个文件?
5h-c9-6f-78-g9-91
这不是有效的mac地址
g
不是十六进制数字。我知道……我是故意修改的……这只是一个例子