博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tcp端口扫描(python多线程)
阅读量:6870 次
发布时间:2019-06-26

本文共 3332 字,大约阅读时间需要 11 分钟。

1 使用单线程扫描单台主机

首先实现的是对单台主机中0-1024端口的扫描,发现差不多每秒扫描一个端口,很慢。

import socketdef tcp_scanner(host,port):    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)    try:        client.connect((host, port))        print('connected to host %s,port %d successfully' %(host,port))        return True    except:        # print('connected to host %s,port %d failed'  %(host,port))        return Falseif __name__ == '__main__':    host = "192.168.10.10"    for port in range(1024):        tcp_scanner(host,port)

  

2 使用多线程扫描单台主机

import threading,socketdef tcp_scanner(host,port):    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)    try:        client.connect((host, port))        print('connected to host %s,port %d successfully' %(host,port))        client.close()        return True    except:        # print('connected to host %s,port %d failed'  %(host,port))        return Falseif __name__ == '__main__':    host = "192.168.10.10"    for port in range(1,1024):        th = threading.Thread(target=tcp_scanner,args=(host,port))        th.start()

  

在运行以上代码时,出现报错:RuntimeError: can't start new thread,原因是超过了线程启动数的上限。

解决办法是修改最大文件描述符打开数。

 

3 使用多线程扫描多台主机

import threading,socket,subprocessdef tcp_scanner(host,port):    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)    try:        client.connect((host, port))        print('connected to host %s,port %d successfully' %(host,port))        client.close()        return True    except:        # print('connected to host %s,port %d failed'  %(host,port))        return Falsedef main():    for i in range(2,255):        # host = "10.224.32.%d" %i        host = "192.168.136.%d" %i        cmd = 'fping %s 1>/dev/null 2>&1;echo $?' %host        p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)        ret = int(p.stdout.read())        if ret == 0:            for port in range(1,4000):                th = threading.Thread(target=tcp_scanner,args=(host,port))                th.start()        else:            print('status of %s:False' %host)if __name__ == '__main__':    main()

  

上面的问题又出现了,可以用的文件描述符不够,即使改到655350。毕竟是全盘扫描。

解决办法是加信号threading.Semaphore,限制线程数

'''create  2018/9/24version 1.0auth    jabbokinfo    scan all the tcp port of the listed hosts by multithreading,to know is it listened'''import threading,socket,subprocess,timescreenLock = threading.Semaphore(value=1)def tcp_scanner(host,port):    socket.setdefaulttimeout(1)    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)    try:        client.connect((host, port))        print('connected to host %s,port %d successfully' %(host,port))        client.close()        screenLock.acquire()        return True    except:        screenLock.acquire()        return False    finally:        screenLock.release()        client.close()def main():    for i in range(2,255):        # host = "10.224.32.%d" %i        host = "192.168.136.%d" %i        cmd = 'fping %s 1>/dev/null 2>&1;echo $?' %host        p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)        ret = int(p.stdout.read())        if ret == 0:            for port in range(1,4000):                th = threading.Thread(target=tcp_scanner,args=(host,port))                th.start()        else:            print('status of %s:False' %host)if __name__ == '__main__':    main()

  

 

转载于:https://www.cnblogs.com/jabbok/p/9696109.html

你可能感兴趣的文章
ASP.net Web API综合示例
查看>>
ie6下a标签使用location.href 不跳转的解决办法
查看>>
向量样本【模式识别】感知器 Perceptron
查看>>
委托杂谈
查看>>
《Android内核剖析》读书笔记 第7章 理解Context
查看>>
IOS开发之UILabel动态高度设置方法
查看>>
儿子购买的书
查看>>
让Android中的webview支持页面中的文件上传
查看>>
hbase regionserver挂掉的问题
查看>>
延迟段创建的学习-实验
查看>>
C/C++ 内存对齐
查看>>
php 在函数内引用全局变量 讲解引用
查看>>
数据结构和算法系列1 线性表之顺序表
查看>>
项目经理的指导
查看>>
android蓝牙开发---与蓝牙模块进行通信
查看>>
HDU 3537 Daizhenyang's Coin(博弈,翻硬币)
查看>>
数据结构和算法系列5 七大排序之冒泡排序和快速排序
查看>>
2013年8月3日第31周六
查看>>
Android使用隐藏api的方法(使用被@hide的api)
查看>>
Robert Penner's Easing Functions
查看>>