Python-implemented multithreaded port scanning Tool sharing _python

Source: Internet
Author: User
Tags clear screen

Last night, I wrote two nights, I finally finished the PY Port Scanner, which is called the 0.1 version, is a Python multi-line Cheng scan tool.

The level is limited, the truth there are some confusion and puzzled place, the code may also write more chaotic. Some problems have not found a good solution, but also hope that we understand. Speed everyone to experiment, I feel OK.

Send the effect figure two copies, respectively, sweep single IP and sweep IP segment:

Source:

Copy Code code as follows:

#-*-Coding:utf-8-*-
__author__ = ' phtih0n '
Import threading, sockets, SYS, CMD, OS, Queue

#扫描常用端口
Portlist = [21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015]
#得到一个队列
def getqueue (list):
Portqueue = Queue.queue (65535)
For P in list:
Portqueue.put (P)
Return Portqueue

#单IP扫描线程个数
Nthread = 20
#线程锁
Lock = Threading. Lock ()
#超时时间
Timeout = 3.0
#打开的端口列表
Openport = []

Class Scanthread (threading. Thread):
def __init__ (self, Scanip):
Threading. Thread.__init__ (self)
Self. IP = Scanip

def Ping (self, Port):
Global Openport, Lock, Timeout
Sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
Sock.settimeout (Timeout)
Address = (self. IP, Port)
Try
Sock.connect (Address)
Except
Sock.close ()
Return False
Sock.close ()
Openport.append (Port)
If Lock.acquire ():
Print "ip:%s port:%d"% (self. IP, Port)
Lock.release ()
Return True


Class Scanthreadsingle (Scanthread):
def __init__ (self, Scanip, singlequeue):
Scanthread.__init__ (self, Scanip)
Self. Singlequeue = Singlequeue

def run (self):
While not self. Singlequeue.empty ():
p = self. Singlequeue.get ()
Self. Ping (P)


Class Scanthreadmulti (Scanthread):
def __init__ (self, Scanip, portlist):
Scanthread.__init__ (self, Scanip)
Self. List = portlist[:]

def run (self):
For p in self. List:
Self. Ping (P)

Class Shell (cmd. CMD):
U ' Py Port Scanner 0.1 usage Instructions:
Port [Port ...] sets the ports scanned, separated by commas.
Default: 21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015
Example:port 21,23,25
Example:port 1000..2000
Example:port 80,443,1000..1500
Scan [IP] scans an IP address
Example:scan 192.168.1.5
Search [IP Begin]-[ip end] scans an IP segment
Example:search 192.168.1.1-192.168.1.100
Time [Timeout] sets timeout, defaults to 3 seconds
Example:time 5
CLS Clear Screen Content
Listport Print Port List
Help Open this assistance
'''
def __init__ (self):
Cmd.cmd.__init__ (self)
Reload (SYS)
Sys.setdefaultencoding (' Utf-8 ')
Self.prompt = "Port Scan >>"
Self.intro = "Py Port Scanner 0.1"

def do_eof (self, line):
Return True

def do_help (self, line):
Print self.__doc__

#设置端口
def do_port (self, line):
Global Portlist
Portlist = []
listtmp = Line.split (', ')
For Port in Listtmp:
If Port.find ("..") < 0:
If not Port.isdigit ():
Print "Input Error"
Return False
Portlist.append (int (port))
Else
Rangelst = Port.split ("..")
If not (Rangelst[0].isdigit () and Rangelst[1].isdigit ()):
Raise ValueError
Exit ()
For I in range (int (rangelst[0]), int (rangelst[1])):
Portlist.append (i)

def do_scan (self, line):
Global Nthread, Portlist
Threadlist = []
StrIP = line
Singlequeue = Getqueue (portlist)
For I in range (0, Nthread):
t = Scanthreadsingle (StrIP, Singlequeue)
Threadlist.append (t)
For T in Threadlist:
T.start ()
For T in Threadlist:
T.join ()

def do_search (self, line):
Global Nthread, Portlist
Threadlist = []
(Beginip, EndIP) = Line.split ("-")
Try
Socket.inet_aton (Beginip)
Socket.inet_aton (ENDIP)
Except
Print "Input Error"
Return
IPRange = Beginip[0:beginip.rfind ('. ')]
begin = Beginip[beginip.rfind ('. ') + 1:]
End = Endip[endip.rfind ('. ') + 1:]
For I in range (int (begin), int (end)):
StrIP = "%s.%s"% (iprange, i)
t = Scanthreadmulti (StrIP, Portlist)
Threadlist.append (t)
For T in Threadlist:
T.start ()
For T in Threadlist:
T.join ()

def do_listport (self, line):
Global Portlist
For P in Portlist:
Print P,
print ' \ n '

def do_time (self, line):
Global Timeout
Try
Timeout = float (line)
Except
Print U "parameter Error"

def do_cls (self, line):
Os.system ("CLS")


If ' __main__ ' = __name__:
Try
Os.system ("CLS")
Shell = Shell ()
Shell.cmdloop ()
Except
Exit ()

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.