# -*- coding=utf-8 -*-
#本模块由乾颐堂陈家栋编写,用于乾颐盾Python课程!
#QQ: 594284672
#亁颐堂官网www.qytang.com
#乾颐盾课程包括传统网络安全(防火墙,IPS...)与Python语言和黑客渗透课程!
import nmap
import optparse
openPorts = []
def nmapScan(targetHost, targetPort):
nmScan = nmap.PortScanner()
nmScan.scan(targetHost, targetPort)
state = nmScan[targetHost]['tcp'][int(targetPort)]['state']
print('*** ' + targetHost + ' tcp端口' + targetPort + ' ' + state)
if state in 'open': openPorts.append(targetPort)
def main():
parser = optparse.OptionParser('用法:\n python3 cjdnmapscan.py -H 目标主机 --pb 起始端口 --pe 结束端口')
parser.add_option('-H', dest = 'targetHost', type = 'string', help = '指定目标主机')
parser.add_option('--pb', dest = 'beginPort', type = 'string', help = '指定扫描端口的起始端口')
parser.add_option('--pe', dest = 'endPort', type = 'string', help = '指定扫描端口的结束端口')
(options, args) = parser.parse_args()
targetHost = options.targetHost
beginPort = options.beginPort
endPort = options.endPort
if (targetHost==None) | (beginPort==None) | (endPort==None):
print(parser.usage)
exit(0)
while int(beginPort) <= int(endPort):
nmapScan(targetHost, beginPort)
beginPort = str(int(beginPort)+1)
tmpFile = open('openPortsFile', 'w')
tmpFile.write(str(openPorts))
tmpFile.close()
if __name__ == '__main__':
main()
转载于:https://my.oschina.net/chenjiadong/blog/657458