安装nmap模块挺麻烦的,搞了半天
不仅要安装pip install nmap
还要sudo apt install nmap
给出代码,没有设多线程,有点慢,注意端口的类型转换,搞了很久
#!/usr/bin/env python # -*- coding:UTF-8 -*- import optparse import nmap #usage:python scan.py -H 119.75.217.26 -p 3306 80 22 def nampScan(tgtHost,tgPort): nmScan = nmap.PortScanner() try: result = nmScan.scan(tgtHost,str(tgPort)) state = result[‘scan‘][tgtHost][‘tcp‘][int(tgPort)][‘state‘] service = result[‘scan‘][tgtHost][‘tcp‘][int(tgPort)][‘product‘] version = result[‘scan‘][tgtHost][‘tcp‘][int(tgPort)][‘version‘] print "[*]" + tgtHost + " tcp/" + str(tgPort) + " " + state + " " + service + " " + version except: pass def main(): parse = optparse.OptionParser(‘usage %prog -H <targethost> -p <targetport>‘) parse.add_option(‘-H‘,dest = ‘tgtHost‘,type = ‘string‘,help = ‘specify target host‘) parse.add_option(‘-p‘,dest = ‘tgtPort‘,type = ‘int‘,help = ‘specify target port‘) (option,args) = parse.parse_args() tgtHost = option.tgtHost tgtPort = str(option.tgtPort) args.append(tgtPort) if(tgtHost == None)|(tgtPort == None): print "[-]usage : python scan.py -H 192.168.2.3 -p 3306 80 22" exit(0) for tgport in args: nampScan(tgtHost,tgport) if __name__ == ‘__main__‘: main()
原文地址:https://www.cnblogs.com/kunspace/p/10599256.html
时间: 2024-10-17 04:01:23