我注意到 python-nmap ,我可以这样做:
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nmap
>>> n = nmap.PortScanner()
>>> n.scan(hosts='192.168.1.0/24', arguments='-sP')
{'nmap': {'scanstats': {'uphosts': u'6', 'timestr': u'Sat Oct 4 21:10:10 2014', 'downhosts': u'250', 'totalhosts': u'256', 'elapsed': u'2.69'}, 'scaninfo': {}, 'command_line': u'nmap -oX - -sP 192.168.1.0/24'}, 'scan': {u'192.168.1.104': {'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': u'Mikes-MBP', 'addresses': {u'ipv4': u'192.168.1.104'}}, u'192.168.1.131': {'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': u'Aidana', 'addresses': {u'ipv4': u'192.168.1.131'}}, u'192.168.1.133': {'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': u'vm-trusty-desktop', 'addresses': {u'ipv4': u'192.168.1.133'}}, u'192.168.1.135': {'status': {'state': u'up', 'reason': u'conn-refused'}, 'hostname': u'android-d79f5b3256db8e11', 'addresses': {u'ipv4': u'192.168.1.135'}}, u'192.168.1.1': {'status': {'state': u'up', 'reason': u'syn-ack'}, 'hostname': '', 'addresses': {u'ipv4': u'192.168.1.1'}}, u'192.168.1.129': {'status': {'state': u'up', 'reason': u'syn-ack'}, 'hostname': u'DiskStation', 'addresses': {u'ipv4': u'192.168.1.129'}}}}
>>> n.all_hosts()
[u'192.168.1.1', u'192.168.1.104', u'192.168.1.129', u'192.168.1.131', u'192.168.1.133', u'192.168.1.135']
但是 nmap 不会泄露 MAC 地址,除非你以 root 身份运行它。然而,当我关闭 Python session 时,我可以立即运行 arp -an 并获取所有找到的主机及其相应 MAC 地址的转储。
有没有一种干净的方法可以直接在Python中获取这些数据,而不需要
- 需要是root用户,或者
- 必须手动解析
arp
输出?
谢谢。
最佳答案
来自 python-nmap 的 example.py:
# Vendor list for MAC address
nm.scan('192.168.0.0/24', arguments='-O')
for h in nm.all_hosts():
if 'mac' in nm[h]['addresses']:
print(nm[h]['addresses'], nm[h]['vendor'])
关于python - 如何在 python 中从附近主机检索 MAC 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26198714/