Python implements network segment fixed port scan __python

Source: Internet
Author: User


A gadget that uses Python to scan a fixed port to receive parameters: Start IP, terminate IP, target port. and test success, so in this record, Daniel do not spray.

#coding =utf-8 Import Socket import time import sys def portscanner (ip,port): Server = (ip,port) SOCKFD = Socket.s Ocket (Socket.af_inet,socket. SOCK_STREAM) Sockfd.settimeout (0.1) ret = SOCKFD.CONNECT_EX (server) #返回0则成功 if not ret:sockfd.close ( ) print '%s:%s is opened ... '% (ip,port) else:sockfd.close () pass return ' Def ip2num (i
    P): LP = [INT (x) for x in Ip.split ('. ')] Return lp[0] << 24 | LP[1] << 16 | LP[2] << 8 |lp[3] def num2ip (num): IP = [', ', ', ', '] ip[3] = (num & 0xff) ip[2] = (num & 0xff0 0) >> 8 ip[1] = (num & 0xff0000) >> ip[0] = (num & 0xff000000) >> return '%s.%  s.%s.%s '% (ip[0],ip[1],ip[2],ip[3]) def iprange (ip1,ip2): Num1 = Ip2num (ip1) num2 = Ip2num (IP2) tmp = num2-  NUM1 if tmp < 0:return None Else:return num1,num2,tmp if __name__ = ' __main__ ': print ' Start time:%s '% tIme.ctime (Time.time ()) If Len (SYS.ARGV) < 4:print ' Usage:scanner01 startip endip Port ' Sys.exit () res = () StartIP = sys.argv[1] EndIP = sys.argv[2] Port = Int (sys.argv[3)) res = IPRange (startip,endip If not Res:print ' endip must to bigger than Startone ' Sys.exit () elif res[2] = = 0:port
            Scanner (Startip,port) else:for x in xrange (int (res[2)) +1: Startipnum = Ip2num (StartIP) Startipnum = startipnum + x portscanner (Num2ip (startipnum), port) print ' End time:%s '% Time.ctime (Tim

 E.time ())

Test the 3389-port opening of a network segment ... The results are as follows:


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.