1. Python penetration test-scan port (note)

Source: Internet
Author: User

being a warrior is not a simple thing, it is an endless struggle that lasts until the last moment of our lives. No life down is a warrior, like no life down is doomed mediocre, is we let ourselves become such or that!
--Natsume Stone

Design ideas:
Call Optparse. Optionparse () method, build option parser, accept host name (or IP address), scan port list two parameters. Build two functions Portscan and Connscan,portscan resolve the host name to an IP address, and then enumerate each port in the port list using the Connscan function to attempt to connect to the host and print the information for the scanned port.

Main function Code:

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=‘int‘,        help=‘specify target port‘)        (options,args) = parser.parse_args()        tgtHost = options.tgtHost        tgtPort = options.tgtPort        args.append(tgtPort)        if (tgtHost == None) | (tgtPort == None):                print(parser.usage)                exit(0)        portScan(tgtHost,args)

Portscann function Code:

def portScan(tgtHost,tgtPorts):        try:                tgtIP = socket.gethostbyname(tgtHost)        except:                print("[-]Cannot resolve ‘%s‘:Unkown host" % tgtHost)                return        try:                tgtName = socket.gethostbyaddr(tgtIP)                print(‘\n[+]Scan Result for:‘+ tgtName[0])        except:                print(‘\n[+]Scan Result for:‘+ tgtIP)        socket.setdefaulttimeout(1)        for tgtPort in tgtPorts:                print(‘Scanning port‘ + str(tgtPort))                connScan(tgtHost,int(tgtPort))

Connscan function Code:

def connScan(tgtHost,tgtPort):        try:                connSkt = socket.socket(socket.AF_INET,socket.SOCK_STREAM)                connSkt.connect((tgtHost,tgtPort))                connSkt.send(‘ViolenPython\r\n‘)                results = connSkt.recv(100)                print(‘[+]%d/tcp open‘ % tgtPort)                print(‘[+]‘+str(results))                connSkt.close()        except:                print(‘[-]%d/tcp closed‘ % tgtPort)

1. Python penetration test-scan port (note)

Related Article

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.