如何调用python-nmap来实现扫描局域网存活主机(代码)


本文摘自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中func什么意思

Python中time模块与datetime模块的详解

Python 的自省是什么?

Python切片是什么

Python如何修改dataframe列名

Python如何编写阶乘?

linux下修改Python命令的方法示例(附代码)

Python fromkeys函数是什么?fromkeys函数的作用是什么?

Python中while循环打印星星的四种形状

Python里的str是什么意思

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...