python使用nmap端口扫描

2023-01-31 03:01:40 python nmap 端口扫描

第一版:只支持以逗号分隔的端口,不支持端口范围  

Firstly:      sudo apt-get install nmap

Secondly:pip install python-nmap

Thirdly:copy the code bellow to a file like  scan_network.py

  1. #!/usr/bin/env Python

  2. import nmap

  3. import optparse


  4. def nmapScan(tgtHost,tgtPort):

  5.    nmScan = nmap.PortScanner()

  6.    nmScan.scan(tgtHost,tgtPort)

  7.    state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']

  8.    print ("[*] " + tgtHost + " tcp/"+tgtPort +" "+state)


  9. def main():

  10.    parser = optparse.OptionParser('usage %prog '+\

  11.                                   '-H <target host> -p <target port>')

  12.    parser.add_option('-H', dest='tgtHost', type='string',\

  13.                      help='specify target host')

  14.    parser.add_option('-p', dest='tgtPort', type='string',\

  15.                      help='specify target port[s] separated by comma')

  16.    

  17.    (options, args) = parser.parse_args()

  18.    

  19.    tgtHost = options.tgtHost

  20.    tgtPorts = str(options.tgtPort).split(',')

  21.    

  22.    if (tgtHost == None) | (tgtPorts[0] == None):

  23.        print (parser.usage)

  24.        exit(0)

  25.    for tgtPort in tgtPorts:

  26.        nmapScan(tgtHost, tgtPort)



  27. if __name__ == '__main__':

  28.    main


Forthly:chmod +x  scan_network.py   

fifthly:      ./scan_network.py -H 192.168.1.1 -p 22,23


第二版:支持以逗号分割及以-分割的端口范围

#!/usr/bin/env python import nmap import optparse def nmapScan(tgtHost,tgtPort):    nmScan = nmap.PortScanner()    nmScan.scan(tgtHost,tgtPort)    state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']    print ("[*] " + tgtHost + " tcp/"+tgtPort +" "+state) def main():    parser = optparse.OptionParser('usage %prog '+\                                   '-H <target host> -p <target port>')    parser.add_option('-H', dest='tgtHost', type='string',\                      help='specify target host')    parser.add_option('-p', dest='tgtPort', type='string',\                      help='specify target port[s] separated by comma')    (options, args) = parser.parse_args()    tgtHost = options.tgtHost ######this code bellow is to support scan port range like 66-88    tgtPorts = []    tgtPorts_cache = str(options.tgtPort).split(',')    i = int(len(tgtPorts_cache))    for m in range( 0,i ):        tgtPorts_split = str(tgtPorts_cache[m]).split('-')        if(len(tgtPorts_split) < 2):            tgtPorts.extend(tgtPorts_split)            #print(tgtPorts)        else:            for n in range(int(tgtPorts_split[0]),int(tgtPorts_split[1])+1):                tgtPorts.append(str(n))                #print(tgtPorts) ######above the tgtPorts are the ports list you want to scann    #tgtPorts = str(options.tgtPort).split(',')        if (tgtHost == None) | (tgtPorts[0] == None):        print (parser.usage)        exit(0)    for tgtPort in tgtPorts:        nmapScan(tgtHost, tgtPort) if __name__ == '__main__':    main()

wKiom1jI5Zvi3unaAAAleECT6Uw115.png-wh_50

相关文章