我注意到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']
但是除非你以root用户身份运行,否则nmap不会咳出MAC地址。然而,当我关闭Python会话时,我可以立即运行arp -an
并获取所有找到的主机及其相应MAC地址的转储。
是否有一种干净的方法可以直接在Python中获取此数据,而无需
arp
输出?感谢。
答案 0 :(得分:3)
来自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'])
答案 1 :(得分:1)
@mikepurvis在Linux上是正确的,我认为是Mac。您需要以root用户身份运行python才能使用nmap获取mac信息。我刚刚在Server 2019上尝试过的Windows上的安装效果很好,但是我的用户帐户在Administrator组中。
我在GitHub中发布了一个示例cli python脚本。此示例包括如何通过shell脚本使用pyenv以干净的方式作为根运行python。
这是使用nmap抓取信息的逻辑的一片段
/**
* Function to be called by a trigger
*
* @param {Object} event Event object
*/
function myTrigger(event){
myFirstFunction(event); // This could be the function that send the form data to an spreadsheet assumin that event holds the form data
mySecondFunction(event); // This could be the function that send an email
myThirdFunction(); // This could be another function
}