python 主机端口扫描器nmap

import nmap
import optparse

def nmapScan(tgtHost,tgtPort):
#创建一个PortScanner()类对象
nmScan = nmap.PortScanner()

#调用PortScanner类的scan()函数,将目标和端口作为参数输入并进行nmap扫描
nmScan.scan(tgtHost,tgtPort)

#输出扫描结果中的状态信息
state = nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
print('[*] ' + tgtHost + " tcp/" + tgtPort + " " + state)

def main():
parser=optparse.OptionParser("[*] Usage : ./nmapScan.py -H -p ")
parser.add_option('-H',dest='tgtHost',type='string',help='specify target host')
parser.add_option('-p',dest='tgtPorts',type='string',help='specify target port[s]')
(options,args)=parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPorts).split(',')
if (tgtHost == None) | (tgtPorts[0] == None):
print(parser.usage)
exit(0)
for tgtPort in tgtPorts:
nmapScan(tgtHost,tgtPort)

if name == 'main':
main()

你可能感兴趣的:(python 主机端口扫描器nmap)