Home  >  Article  >  Backend Development  >  Two ways to use nmap port scanning in Python

Two ways to use nmap port scanning in Python

高洛峰
高洛峰Original
2017-03-22 13:59:282713browse

First version: Only comma-separated ports are supported, port ranges are not supported

Firstly: sudo apt-get install nmap

Secondly: pip install python -nmap

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

#!/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 '+\
                                   &#39;-H <target host> -p <target port>&#39;)
    parser.add_option(&#39;-H&#39;, dest=&#39;tgtHost&#39;, type=&#39;string&#39;,\
                      help=&#39;specify target host&#39;)
    parser.add_option(&#39;-p&#39;, dest=&#39;tgtPort&#39;, type=&#39;string&#39;,\
                      help=&#39;specify target port[s] separated by comma&#39;)
    
    (options, args) = parser.parse_args()
    
    tgtHost = options.tgtHost
    tgtPorts = str(options.tgtPort).split(&#39;,&#39;)
    
    if (tgtHost == None) | (tgtPorts[0] == None):
        print (parser.usage)
        exit(0)
    for tgtPort in tgtPorts:
        nmapScan(tgtHost, tgtPort)


if name == &#39;main&#39;:
    main

Forthly:chmod +x scan_network.py

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

Second version: supports comma-separated and --separated ports Range

#!/usr/bin/env python
import nmap
import optparse
def nmapScan(tgtHost,tgtPort):
    nmScan = nmap.PortScanner()
    nmScan.scan(tgtHost,tgtPort)
    state=nmScan[tgtHost][&#39;tcp&#39;][int(tgtPort)][&#39;state&#39;]
    print ("[*] " + tgtHost + " tcp/"+tgtPort +" "+state)

def main():
    parser = optparse.OptionParser(&#39;usage %prog &#39;+\
                                   &#39;-H <target host> -p <target port>&#39;)
    parser.add_option(&#39;-H&#39;, dest=&#39;tgtHost&#39;, type=&#39;string&#39;,\
                      help=&#39;specify target host&#39;)
    parser.add_option(&#39;-p&#39;, dest=&#39;tgtPort&#39;, type=&#39;string&#39;,\
                      help=&#39;specify target port[s] separated by comma&#39;)

    (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(&#39;,&#39;)
    i = int(len(tgtPorts_cache))
    for m in range( 0,i ):
        tgtPorts_split = str(tgtPorts_cache[m]).split(&#39;-&#39;)
        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(&#39;,&#39;)    
    if (tgtHost == None) | (tgtPorts[0] == None):
        print (parser.usage)
        exit(0)
    for tgtPort in tgtPorts:
        nmapScan(tgtHost, tgtPort)


if name == &#39;main&#39;:
    main()

Two ways to use nmap port scanning in Python

The above is the detailed content of Two ways to use nmap port scanning in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn