import nmap
import optparse

def nmap_scan(tgtHost, tgtPort):
    nm_scan = nmap.PortScanner()

    # use default: nmap -sV (TCP open port scan with SYN packet) 
    nm_scan.scan(tgtHost, tgtPort)
    state=nm_scan[tgtHost]['tcp'][int(tgtPort)]['state']
    print(" [*] {} tcp/{} {}".format(tgtHost, tgtPort, state))


def main():
    # for usage that this program
    parser = optparse.OptionParser('usage %prog -H <target host> -p <target port>')

    # create options
    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')

    # parse args and options then sign var 
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts= str(options.tgtPort).split(', ')
    
    if (tgtHost==None) or (tgtPorts[0]==None):
        print(parser.usage)
        exit(0)
    
    for tgtPort in tgtPorts:
        nmap_scan(tgtHost, tgtPort)

if __name__ == '__main__':
    main()

