1. 程式人生 > >端口掃描器--利用python的nmap模塊

端口掃描器--利用python的nmap模塊

bsp spec service .py try clas def class 多線程

安裝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()

端口掃描器--利用python的nmap模塊