用于扫描开放了某些端口的ip
timeout可以设置成1秒或2秒。
local_ips是获取多块网卡上绑定的IP,比如我的IP地址是192.168.1.4和192.168.56.1。
而代码所做的事情就是扫描 [192.168.1.1 ~ 192.168.1.254] [192.168.56.1 ~ 192.168.56.254] 有哪些IP开放80端口。
虽然有许多现成的扫描工具
但是喜欢PY交易的童鞋不妨可以研究研究
import socket
import threading
routers = []
lock = threading.Lock()
def search_routers():
routers = []
local_ips = socket.gethostbyname_ex(socket.gethostname())[2] # get local IP
all_threads = []
for ip in local_ips:
for i in range(1, 255):
array = ip.split('.')
array[3] = str(i)
new_ip = '.'.join(array)
t = threading.Thread(target=check_ip, args=(new_ip,) )
t.start()
all_threads.append(t)
for t in all_threads:
t.join()
def check_ip(new_ip):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
result = s.connect_ex((new_ip, 80))
s.close()
if result == 0:
lock.acquire()
print new_ip.ljust(15), ' port 80 is open'
routers.append((new_ip, 80))
lock.release()
print 'Searching for routers, please wait...'
search_routers()
你可能喜欢pyrasite – 向python进程注入代码工具ISF:一款基于Python的工控系统漏洞利用框架DIY简易Python脚本调用AWVS扫描
领取专属 10元无门槛券
私享最新 技术干货