本文摘自php中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于如何调用python-nmap来实现扫描局域网存活主机(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。使用环境:Raspberry 3b+ +netifaces+python-nmap+nmap
调用netifaces自动获取ip地址:
1 2 | def get_gateways():
return netifaces.gateways()[ 'default' ][netifaces.AF_INET][0]
|
将ip地址生成一个网段中所有ip地址的列表:
1 2 3 4 5 | def get_ip_lists(ip):
ip_lists = []
for i in range(1, 256):
ip_lists.append( '{}{}' .format(ip[:-1], i))
return ip_lists
|
主要实现代码及数据分割:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def main(ip=None):
ip=get_gateways()
ip_lists=get_ip_lists(ip)
nmScan,temp_ip_lists,hosts = nmap.PortScanner(),[],ip[:-1]+ '0/24'
ret = nmScan.scan(hosts=hosts, arguments= '-sP' )
print ( '扫描时间:' +ret[ 'nmap' ][ 'scanstats' ][ 'timestr' ]+ '\n命令参数:' +ret[ 'nmap' ][ 'command_line' ])
for ip in ip_lists:
print ( 'ip地址:' +ip+ ':' )
if ip not in ret[ 'scan' ]:
temp_ip_lists.append(ip)
print ( '扫描超时' )
else : print ( '已扫描到主机,主机名:' +ret[ 'scan' ][ip][ 'hostnames' ][0][ 'name' ])
print (str(hosts) + ' 网络中的存活主机:' )
for ip in temp_ip_lists:ip_lists.remove(ip)
for ip in ip_lists: print (ip)
|
完整代码:
阅读剩余部分
相关阅读 >>
Python适合网页编程吗
Python教你高效办公,自制屏幕翻译工具
Python是怎么计算auc指标的?
Python开发tornado网站之requesthandler:接入点函数
Python闰年判定代码是什么
Python什么是递归?两种优先搜索算法的实现 (代码示例)
Python怎样求得最大公约数
Python 实现在txt指定行追加文本的方法
Python中有for函数吗
Python文件操作a+与a模式的区别
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何调用python-nmap来实现扫描局域网存活主机(代码)