Python_nmap_info

python-nmap库说明

python-nmap库的官方说明文档,作者给出了一个实例代码

实践代码

参考这两个资料,写出了如下的功能代码:

# @Time : 2020/11/11 15:56
# @Author : alee
# @File : nmap_scanner.py
# @Software : PyCharm
# @Object : scanning with python-nmap
import nmap,sys
from time import time

start_time = time()
for i in range(1, len(sys.argv)):
    flag = sys.argv[i].split('-')
    if 'u' in flag:
        scan_host = sys.argv[i + 1]
    elif 'p' in flag:
        scan_ports = sys.argv[i + 1]
try:
    nm = nmap.PortScanner()
except:
    print('python-nmap not found')
    sys.exit(1)
try:
    nm.scan(scan_host,scan_ports)
except:
    print('usage: nmap_scanner.py -u ip -p port')
    sys.exit(1)
print(nm.scaninfo())
run_time = (time() - start_time)
print("running time: %0.2f seconds." % run_time)

代码功能很简单,就是使用-u引入ip地址,-p引入扫描端口,然后就调用nmap进行扫描。

说明

针对该库,主要需要了解如下的一些输出形式:(nm = nmap.PortScanner())