时间:2022-10-03 11:34:30 | 栏目:Python代码 | 点击:次
引用Nmap库实现扫描功能,本节课比较简单一看就会。
编写环境:Python2.x
首先安装Nmap程序,并添加环境变量
pip install nmap pip install python-nmap
import nmap
def nmapScan(host,port):
nmScan=nmap.PortScanner() #实例化
state = nmScan.scan(host,port) #scan() 方法扫描端口
print state
nmapScan('127.0.0.1','80')
如果想加nmap参数,直接在后面可以加上,第三个参数可选
state = nmScan.scan(host,port,arguments='-O') #scan()

实例化后 调用scan方法进行扫描


输出的内容非常详细,我们可以截取我们想要的内容先说一下几个方法
nmScan=nmap.PortScanner() #实例化
nmScan.scan(host,port) #scan() 方法扫描端口
print nmScan.command_line() #输出命令
print nmScan.scaninfo() #返回nmap扫描信息,为字典类型
print nmScan.all_hosts() #返回nmap扫描信息,为列表类型
根据自己需求,输出自己想要的内容

我们这里选择默认的json格式
print state['scan'][host]['tcp'][int(port)]['state']
