当前位置: 移动技术网 > IT编程>脚本编程>Python > Python实现的多线程端口扫描工具分享

Python实现的多线程端口扫描工具分享

2019年06月14日  | 移动技术网IT编程  | 我要评论

曲啸,小方块超级玛丽,与卓越同行

昨晚今晚写了两晚,总算把py port scanner 写完了,姑且称之为0.1版本,算是一个python多线程端口扫描工具。

水平有限,实话中间有一些困惑和不解的地方,代码可能也写的比较乱。有些问题并未找到很好的解决方法,还望大家谅解。速度大家自己试验,我感觉还行。

送上效果图两份,分别是扫单ip和扫ip段:

源码:

复制代码 代码如下:

# -*- coding: utf-8 -*-
__author__ = 'phtih0n'
import threading, socket, 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 使用说明:
    port [port..] 设置扫描的端口,用逗号分隔。
        默认: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] 扫描某一ip地址
        example: scan 192.168.1.5
    search [ip begin]-[ip end] 扫描某一ip段
        example: search 192.168.1.1-192.168.1.100
    time [timeout] 设置超时时间,默认为3秒
        example: time 5
    cls 清楚屏幕内容
    listport 打印端口列表
    help 打开本帮助
        '''
    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 "输入错误"
                    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 "输入错误"
            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"参数错误"

    def do_cls(self, line):
        os.system("cls")


if '__main__' == __name__:
    try:
        os.system("cls")
        shell = shell()
        shell.cmdloop()
    except:
        exit()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网