资源简介
个人用python写的扫描主机端口的小程序,可用于扫描一台指定主机开放且正在监听哪些端口,请注意,在一台主机上netstat -an看到的正在listening的端口未必是开放的,还要看主机的防火墙以及路由器的防火墙的设置。脚本中可以配置的参数说明,可以自行修改:
gd.Host = "127.0.0.1"
#要被扫描的主机IP
gd.ThreadSize = 1000
#并行线程数,建议值1000-3000
gd.ExitNum = 10
#扫描到多少个开放端口后退出
gd.Timer = 5
#超时时间,内网设1,公网不超过5
gd.PortList_1 = range(1,2000)
#端口范围
gd.PortList_2 = [3389,6000]
#特殊端口列表
脚本开始运行后会打印扫描到的主机正在监听且开放的端口,按ctrl+c随时结束扫描。
代码片段和文件信息
#!/usr/bin/python
import ossystimesocketthreadingcopy
class cGlobalData:
pass
gd=cGlobalData()
gd.Host = “127.0.0.1“
gd.ThreadSize = 1000
gd.ExitNum = 100
gd.Timer = 5
gd.PortList_1 = range(30000)
gd.PortList_2 = [33896000]
gd.OpenPorts = []
gd.NewPorts = []
gd.PrintLock = threading.Lock()
gd.UpdateLock = threading.Lock()
gd.OpenLock = threading.Lock()
gd.ThreadLock = threading.Lock()
gd.ExitLock = threading.Lock()
gd.ScanPorts = []+gd.PortList_1 + gd.PortList_2
gd.ScanedNum = 0
gd.Exit = False
gd.ThreadNum=0
gd.GroupSize=int((len(gd.ScanPorts))/gd.ThreadSize)
class cScanThread(threading.Thread):
def __init__(selfaIdxaLenaId):
threading.Thread.__init__(self)
self.vId = aId
self.vIdx=aIdx
self.vLen=aLen
return
def run(self):
global gd
gd.PrintLock.acquire()
#print “thread“self.vId“scan from“self.vIdx“to“self.vIdx+self.vLen
gd.PrintLock.release()
for i in range(self.vIdxself.vIdx+self.vLen):
gd.ExitLock.acquire()
vExit=gd.Exit
gd.ExitLock.release()
if(vExit):
gd.ThreadLock.acquire()
gd.ThreadNum = gd.ThreadNum -1
gd.ThreadLock.release()
gd.PrintLock.acquire()
#print “thread“self.vId“exit because ports enough“
gd.PrintLock.release()
return
sk = socket.socket(socket.AF_INET socket.SOCK_STREAM)
sk.settimeout(gd.Timer)
try:
sk.connect((gd.Hostgd.ScanPorts[i]))
except Exception:
gd.UpdateLock.acquire()
gd.ScanedNum = gd.ScanedNum + 1
gd.UpdateLock.release()
sk.close()
continue
sk.close()
gd.PrintLock.acquire()
print ‘******************‘+str(gd.ScanPorts[i])+‘***‘
gd.PrintLock.release()
gd.OpenLock.acquire()
gd.NewPorts.append(gd.ScanPorts[i])
gd.OpenLock.release()
gd.ThreadLock.acquire()
gd.ThreadNum = gd.ThreadNum -1
gd.ThreadLock.release()
gd.PrintLock.acquire()
#print “thread“self.vId“scan over“
gd.PrintLock.release()
return
def main():
global gd
try:
pass
for i in range(gd.ThreadSize):
gd.ThreadLock.acquire()
gd.ThreadNum = gd.ThreadNum + 1
vTrd = cScanThread(i*gd.GroupSizegd.GroupSizegd.ThreadNum)
gd.ThreadLock.release()
vTrd.start()
if(len(gd.ScanPorts)-(gd.ThreadSize*gd.GroupSize) > 0):
gd.ThreadLock.acquire()
gd.ThreadNum = gd.ThreadNum + 1
vTrd = cScanThread((gd.ThreadSize-1)*gd.GroupSizelen(gd.ScanPorts)-(gd.ThreadSize*gd.GroupSize)gd.ThreadNum)
gd.ThreadLock.release()
vTrd.start()
gd.ThreadLock.acquire()
vThreadNum = gd.ThreadNum
gd.ThreadLock.release()
gd.PrintLock.acquire()
#print vThreadNum“threads“
#print gd.GroupSize“GroupSize“
gd.PrintLock.release()
vOldNum=0
vOpenNum=0
while(True):
time.sleep(2)
gd.ThreadLock.acquire()
vThreadNum = gd.ThreadNum
gd.ThreadLock.release()
gd.UpdateLock.acquire()
vScanedNum = gd.ScanedNum
gd.UpdateLock.release()
if(vOldNum == vScanedNum):
if(vThreadNum <= 0):
break
continue
print vScaned
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3720 2010-03-30 16:27 scanport.py
----------- --------- ---------- ----- ----
3720 1
相关资源
- 最全Python编程基础+简单爬虫+进阶项目
- python wxpy实现微信群消息转发
- Python_验证采样定理.py
- python爬虫爬微信公众号文章
- Python网络爬虫实战Scrapy.txt
- micropython下的ds18b20代码
- knn 字符识别 python
- EM算法Python实现
- 简单的python购物车程序
- 是AI就躲个飞机-纯Python实现人工智能
- Python接口测试框架实战与自动化进阶
- 基本蚁群算法python实现
- python核心编程第二版习题答案
- 基于二维伽马函数的光照不均匀的图
- python主题爬取百度新闻
- MySQL-python-1.2.5.win-amd64-py2.7
- nao机器人单个关节运动程序
- Python - Flask 使用Ajax 实现多文件上传
- Python魔鬼训练营系列教程
- 多线程爬虫
- python简单实现-中国象棋
- Python爬虫--抓取百度百科的前1000个页
- nlp肯定句与否定句判断
- 绘制社交网络图的幂律分布python代码
- Python 飞机大战 游戏设计需求 与 实现
- PARZEN窗和K近邻算法的python实现
- windows下fcntl.py
- Python项目案例开发从入门到实战源代
- Python项目案例开发从入门到实战源代
- python写的专家系统玩具分类
评论
共有 条评论