Simple port scanner written in Python

Source: Internet
Author: User

1 #coding = Utf-82 3 " "4 python 3.45 a simple TCP port connection is given to determine whether the specified IP is open for the specified port. 6 " "7 8 ImportSocket9 ImportOptparseTen ImportRe One ImportThreading A ImportSYS -  - defAnlyze_host (target_host): the #Converts the target value obtained from the--host parameter to the standard xxx.xxx.xxx.xxx form, in which the gethostbyname function of the socket is used to convert the value of the domain name form to a four-bit point binary form -     Try: -Pattern = Re.compile (r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')#matching standard point-in-IP -Match =Pattern.match (target_host) +         ifmatch: -             return(Match.group ()) +         Else: A             Try: atTarget_host = Socket.gethostbyname (target_host)#if not, parse the value of Target_host as the domain name -                 return(target_host) -             exceptException as err: -                 Print('Address resolution Error:', Err) - exit (0) -     exceptException as err: in         Print('Please note that error 1:', Sys.exc_info () [0],err] -         Print(parser.usage) to exit (0) +                      -  the defAnlyze_port (target_port): * #resolves the value passed in the--port parameter and returns a list of ports $     Try:Panax NotoginsengPattern = Re.compile (r'(\d+)-(\d+)')#Parse connector-pattern -Match =Pattern.match (Target_port) the         ifmatch: +start_port = Int (Match.group (1)) Aend_port = Int (Match.group (2)) the             return([x forXinchRange (Start_port,end_port + 1)]) +         Else: -             return([Int (x) forXinchTarget_port.split (',')]) $     exceptException as err: $         Print('Please note that error 2:', Sys.exc_info () [0],err] -         Print(parser.usage) - exit (0) the  - defScanner (target_host,target_port):Wuyi #Create a Socket object thes =Socket.socket (Socket.af_inet,socket. SOCK_STREAM) -S.settimeout (5) Wu     Try: - S.connect ((target_host,target_port)) About         #S.sendall (b ' hello\r\n\r\n ') $         #message = S.RECV (+) -         #If message: -         Print('[+]%s%3s port: Open'% (Target_host,target_port))#If a connection can be established, it indicates that the port is open -            #print ('%s '% message.decode (' utf-8 ')) A     exceptsocket.timeout: +         Print('[-]%s%3s port: Off'% (Target_host,target_port))#if the connection times out, this port is closed the     exceptException as err: -         Print('Please note that error 3:', Sys.exc_info () [0],err] $ exit (0) the  the  the          the defMain (): -Usage ='usage:%prog-h ' inParser = Optparse. Optionparser (usage,version='%prog v1.0') theParser.add_option ('--host', dest='Target_host', type='string', thehelp='host, domain, or IP that needs to be scanned') AboutParser.add_option ('--port', dest='Target_port', type='string', thehelp='host ports that need to be scanned, support 1-100 or 21,53,80 two forms') the(Options,args) =Parser.parse_args () the    ifOptions.target_host = = NoneorOptions.target_port = =None: +       Print(parser.usage) - exit (0) the    Else:BayiTarget_host =Options.target_host theTarget_port =Options.target_port the  -Target_host =anlyze_host (target_host) -Target_port =Anlyze_port (Target_port) the  the     forPortinchTarget_port: thet = Threading. Thread (target=scanner,args= (Target_host,port))#Multi-threaded scan port the T.start () -  the if __name__=='__main__': the Main ()  

The result of the operation is:

1C:\Python34\python.exe tcpscanner.py--host 192.168.2.1--port 1-10242[+]192.168.2. 1 of 25 Ports: Open3[+]192.168.2. 1 of 110 Ports: Open4[+]192.168.2. 1 of 119 ports: Open5[+]192.168.2. 1 of 143 ports: Open6[+]192.168.2. 1 of 465 ports: Open7[+]192.168.2. 1 of 563 ports: Open8[+]192.168.2. 1 of 587 ports: Open9[+]192.168.2. 1 of 993 ports: OpenTen[+]192.168.2. 1 of 995 ports: Open One[+]192.168.2. 1 of 80 ports: Open A[-]192.168.2. 1 of 1 ports: Off -[-]192.168.2. 1 of 18 ports: off -[-]192.168.2. 1 of 4 ports: Off the[-]192.168.2. 1 of 8 ports: off -[-]192.168.2. 1 of 13 ports: off -[-]192.168.2. 1 of 9 ports: off -[-]192.168.2. 1 of 42 ports: Off +[-]192.168.2. 1 of 19 ports: Off -[-]192.168.2. 1 of 67 ports: Off +[-]192.168.2. 1 of 21 ports: off A[-]192.168.2. 1 of 14 ports: off at[-]192.168.2. 1 of 17 ports: off -......

Simple port scanner written in Python

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.