Two methods for using nmap port scanning in python

Source: Internet
Author: User
This article introduces two methods for using nmap port scanning in python: The first version only supports ports separated by commas and does not support Port ranges.

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 pythonimport nmapimport optparsedef 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 
 
   -p 
  
   ')    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    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
  
 

Forthly: chmod + x scan_network.py

Export thly:./scan_network.py-H 192.168.1.1-p

Version 2: supports comma-separated and-separated port ranges

#!/usr/bin/env pythonimport nmapimport optparsedef 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 
 
   -p 
  
   ')    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()
  
 

The above is the details of the two methods for python to use nmap port scanning. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.