Python implements simple port scanner code instances and python Port Scanners

Source: Internet
Author: User

Python implements simple port scanner code instances and python Port Scanners

I have added some new content on the basis of some information on the Internet. It is a Python socket programming expert.

# Coding = utf-8import socketimport timeimport sysimport structimport threadingfrom threading import Thread, activeCountresults = [] def portScanner (ip, port): server = (ip, port) sockfd = socket. socket (socket. AF_INET, socket. SOCK_STREAM) sockfd. settimeout (0.1) # Set the socket timeout in blocking mode. ret = sockfd. connect_ex (server) #0 is returned for success, and error is returned for failure. If not ret: sockfd. close () results. append ([ip, port]) # print '% s: % s is opened... '% (ip, port) else: sockfd. close () pass return ''def ip2num (ip): # convert an ip address to a numeric lp = [int (x) for x in ip address. split ('. ')] return lp [0] <24 | lp [1] <16 | lp [2] <8 | lp [3] def num2ip (num ): ip = ['',''] ip [3] = (num & 0xff) ip [2] = (num & 0xff00)> 8 ip [1] = (num & 0xff0000)> 16 ip [0] = (num & 0xff000000)> 24 retur N' % s. % s. % s. % s' % (ip [0], ip [1], ip [2], ip [3]) def iprange (ip1, ip2): num1 = socket. ntohl (struct. unpack ("I", socket. inet_aton (str (ip1) [0]) num2 = socket. ntohl (struct. unpack ("I", socket. inet_aton (str (ip2) [0]) tmp = num2-num1 if tmp <0: return None else: return num1, num2, tmpif _ name _ = '_ main _': if (len (sys. argv )! = 4) & (len (sys. argv )! = 2): # Usage description print 'usage: \ n \ tstrap. py startip endip port 'print' \ tstrap. py ip'sys. exit () if len (sys. argv) = 4: # scan time_start = time for an IP segment. time () # Start time startip = sys. argv [1] # Starting IP address endip = sys. argv [2] # end IP port = int (sys. argv [3]) # port number res = iprange (startip, endip) if not res: print 'endip must be bigger than startone' sys. exit () elif res [2] = 0: portScanner (startip, port) else: for x in xrange (int (res [2]) + 1 ): # The IP address increases sequentially by startipnum = ip2num (startip) startipnum = startipnum + x if activeCount () <= 1000: Thread (target = portnum, args = (num2ip (startipnum ), port )). start () print "There are % d hosts. "% len (results) results. sort () for ip, port in results: print "% s: % d is opened... "% (ip, port) times = time. time ()-time_start # print 'use time: % s' % times if len (sys. argv) = 2: time_start = time. time () port = 0 ip = sys. argv [1] while (port <2000): if activeCount () <= 40: # Set 40 Thread scan Thread (target = portcount, args = (ip, port )). start () port = port + 1 results. sort () for ip, port in results: print "% s: % d is opened... "% (ip, port) times = time. time ()-time_start print 'use time: % s' % times

The effect is as follows:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.