用python写的一个多线程的端口扫描

本文介绍了一种用于快速扫描指定IP地址范围内特定端口状态的自动化工具。该工具通过ping命令检测端口连通性,并能处理并发请求以提高效率。详细阐述了其核心算法、线程管理及异常处理机制,适用于网络安全评估和系统维护场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python
import socket
import sys
import threading
import time

NORMAL = 0
ERROR = 1
TIMEOUT = 5

def ping(ip , port , timeout=TIMEOUT):
	try:
		cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		cs.settimeout(float(timeout))
		address=(str(ip),int(port))
		status = cs.connect_ex((address))
		if status == NORMAL :
			print "%d is NORMAL" %port
	except Exception ,e:
		print ERROR
		print "error:%s" %e
		return ERROR
	cs.close()
	return NORMAL

 
class Scan(threading.Thread):
    def __init__(self , ip , timeout):
        threading.Thread.__init__(self)
	self.ip = ip
	self.timeout = timeout

    def run(self):
        global p_begin , p_end , mutex
        threadname = threading.currentThread().getName()
        while 1:
            mutex.acquire()
            p_begin += 1
            if p_begin >  p_end:
		    mutex.release()
		    break
            mutex.release()
	    ping(self.ip , p_begin , self.timeout)

	
 
if __name__ == '__main__':
    if len(sys.argv) < 4:
	print 'format:urlbegin portbegin portend timeout'

    urlbegin = str(sys.argv[1])
    portbegin = int(sys.argv[2])
    portend = int(sys.argv[3])
    timeout = float(sys.argv[4])

    global p_begin , p_end, mutex
    threads = []
    num = 10
    p_begin = portbegin 
    p_end = portend
    mutex = threading.Lock()
    for x in xrange(0 , num):
        t_scan = Scan(urlbegin , timeout)
	t_scan.setDaemon(True)
        threads.append(t_scan)
    for t in threads:
        t.start()
    for t in threads:
        t.join()  


支持多线程快速扫描 支持网段扫描: ScanPort.py 支持自定义网段扫描: ScanPort.py 192.168.2 支持自定义子网段扫描: ScanPort.py 192.168.3.100 192.168.3.120 支持Web端口自动显示链接: C:\>ScanPort.py Scanning [192.168.200.1]-[192.168.200.254] Total 254 ip(s)... 192.168.200.1 80 web http://192.168.200.1 192.168.200.81 80 web http://192.168.200.81 192.168.200.81 443 web https://192.168.200.81 192.168.200.99 80 web http://192.168.200.99 192.168.200.99 443 web https://192.168.200.99 192.168.200.115 80 web http://192.168.200.115 192.168.200.115 443 web https://192.168.200.115 192.168.200.123 80 web http://192.168.200.123 192.168.200.123 443 web https://192.168.200.123 192.168.200.130 80 web http://192.168.200.130 192.168.200.130 443 web https://192.168.200.130 192.168.200.133 80 web http://192.168.200.133 192.168.200.133 443 web https://192.168.200.133 192.168.200.144 80 web http://192.168.200.144 192.168.200.144 443 web https://192.168.200.144 192.168.200.151 8080 web http://192.168.200.151 192.168.200.160 443 web https://192.168.200.160 192.168.200.160 80 web http://192.168.200.160 192.168.200.163 80 web http://192.168.200.163 192.168.200.163 443 web https://192.168.200.163 192.168.200.163 1080 Proxy 192.168.200.168 80 web http://192.168.200.168 192.168.200.168 443 web https://192.168.200.168 192.168.200.168 1080 Proxy 192.168.200.173 8080 web http://192.168.200.173 192.168.200.173 443 web https://192.168.200.173 192.168.200.181 80 web http://192.168.200.181 192.168.200.181 443 web https://192.168.200.181 192.168.200.180 80 web http://192.168.200.180 192.168.200.180 443 web https://192.168.200.180 192.168.200.192 80 web http://192.168.200.192 192.168.200.192 443 web https://192.168.200.192 All RUN TIME : 97.0510001183s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值