[python]nmap端口扫描

import optparse
import nmap
def scan(host,port,values):
 n=nmap.PortScanner()
 n.scan(host,port,values)
 for host in n.all_hosts():
    print('***************')
    print('Host: %s (%s)'%(host, n[host].hostname()))
    print('State: %s'%n[host].state())
    for proto in n[host].all_protocols():
        print('--------------')
        print('Protocols: %s'%proto)
        port=n[host][proto].keys()
        port.sort()
        for po in port:
            print('port: %s\tstate: %s'%(po,n[host][proto][po]['state']))
def op():
    p=optparse.OptionParser()
    p.add_option('-H',dest='HOST',type='string')
    p.add_option('-P',dest='PORTS',type='string')
    p.add_option('-V',dest='VALUES',type='string')
    return(p)
p=op()
(options, args)=p.parse_args()
host=options.HOST
ports=options.PORTS
values=options.VALUES
scan(host,ports,values)