1.直接修改脚本里的端口和ip地址使用
脚本源码:
#coding:utf-8
#端口扫描 利用聊天室客户端,如果端口开发,正常连接,如果没开放报错
import socket
import threadpool
host="127.0.0.1"
def scanner(host,port):
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
print host,port,"isopen"
except:
pass
pool=threadpool.ThreadPool(50)
args=[]
for port in range(1,4000):
args.append(([host,port],None))
# 两个参数 函数和传递的参数列表
reqs=threadpool.makeRequests(scanner,args)
for req in reqs:
pool.putRequest(req)
pool.wait()
效果:
2.命令行输入命令指定参数和ip
命令格式如下:
文件名 端口(80,445,1-2000) ip地址 线程
例:xxx.py 80,10-2000 127.0.0.1 80
脚本源码:
#coding:utf-8
#使用命令 scanner.py 端口(80,445,1-2000) ip地址 线程
import sys
import re
import socket
import threadpool
portstr=sys.argv[1]
ip=sys.argv[2]
tnums=sys.argv[3]
portlist=portstr.split(",")
#单个ip处理
ports=[]
for port in portlist:
if re.match("\d{1,5}$",port):
ports.append(int(port))
#范围处理
else:
fanwei=port.split("-")
list=range(int(fanwei[0]),int(fanwei[1])+1)
for p in list:
ports.append(p)
port0penedList=[]
#扫描
def scanner(host,port):
global port0penedList
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
port0penedList.append((host,port))
except:
pass
#多线程
t=threadpool.ThreadPool(int(tnums))
args=[]
for x in ports:
args.append(([ip,x],None))
reqs=threadpool.makeRequests(scanner,args)
for req in reqs:
t.putRequest(req)
t.wait()
for p in port0penedList:
print p,"is open"