如何调用python-nmap来实现扫描局域网存活主机(代码)
本篇文章给大家带来的内容是关于如何调用python-nmap来实现扫描局域网存活主机(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
使用环境:Raspberry 3b+ +netifaces+python-nmap+nmap
调用netifaces自动获取ip地址:
def get_gateways(): return netifaces.gateways()['default'][netifaces.AF_INET][0]
将ip地址生成一个网段中所有ip地址的列表:
def get_ip_lists(ip): ip_lists = [] for i in range(1, 256): ip_lists.append('{}{}'.format(ip[:-1], i)) return ip_lists
主要实现代码及数据分割:
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)
完整代码:
#!/usr/bin/python #_*_ coding:utf8 _*_ import netifaces,nmap def get_gateways(): return netifaces.gateways()['default'][netifaces.AF_INET][0] def get_ip_lists(ip): ip_lists = [] for i in range(1, 256): ip_lists.append('{}{}'.format(ip[:-1], i)) return ip_lists 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) if __name__ == '__main__': main()
实验截图:
以上是如何调用python-nmap来实现扫描局域网存活主机(代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
