Python - nmap 扫描而不查找特定端口

Python - nmap scan without looking for specific ports

是否可以使用不关注可用端口的 python 脚本启动 nmap 扫描?

>>> import nmap
>>> nm = nmap.PortScanner()
>>> nm.scan('127.0.0.1', '22-443')
>>> nm.command_line()
'nmap -oX - -p 22-443 -sV 127.0.0.1'

此示例需要扫描一个端口或一系列端口。在 linux 终端的正常 nmap 扫描中,它可以很简单;

nmap -O -v <ip address/range>

这可能吗?

你试过吗

>>> nm.scan('127.0.0.1')

?因为 ports 参数是可选的。

传递任何你想要的参数:

nm.scan(hosts='<ip address/range>',arguments="-O -v")

请记住,指纹识别需要 root 权限。

如果你正在扫描一个范围内的 ip,PortScannerAsync 可能会有用:

import nmap

nm = nmap.PortScannerAsync()
def callback_result(host, scan_result):
    print '------------------'
    print host, scan_result

nm.scan('192.168.1.0/24', arguments="-O -v", callback=callback_result)
while nm.still_scanning():
    print("Waiting >>>")
    nm.wait(2)