创建多个IP文件:
既然是多线程,我们肯定是扫描多个ip,首先我们创建多个ip文件:
>>> f=open("/root/4.txt","w")
>>> for i in range(1,255):
... IP="192.168.1." +str(i)
... f.write(IP + "\n")
...
>>> f.close()
>>> exit()
**
使用脚本批量扫描:
**
root@kali:~# python 30.py 4.txt
192.168.1.1it have not a banner
192.168.1.2it have not a banner
192.168.1.3it have not a banner
192.168.1.4it have not a banner
192.168.1.5it have not a banner
192.168.1.6it have not a banner
192.168.1.7it have not a banner
192.168.1.8it have not a banner
192.168.1.9it have not a banner
192.168.1.10it have not a banner
192.168.1.11it have not a banner
192.168.1.12it have not a banner
192.168.1.13it have not a banner
192.168.1.14it have not a banner
192.168.1.15it have not a banner
192.168.1.16it have not a banner
192.168.1.17it have not a banner
192.168.1.18it have not a banner
192.168.1.19it have not a banner
192.168.1.20it have not a banner //ctrl加z后台运行
root@kali:~# jobs -l
[1]+ 2247 停止 python 30.py 4.txt
root@kali:~# kill -9 2247
root@kali:~#
[1]+ 已杀死 python 30.py 4.txt 强制结束
通常都是通过多线程的方式来执行一个函数,从而使得该函数中的代码可以并发执行。
t=Thread(target=要执行的函数,args=(向函数传递的参数))
t.start()
#!/usr/bin/python
#coding=utf-8
import socket
import sys
import os
from threading import Thread
if len(sys.argv)!=2:
print "请正确使用文件:" + sys.argv[0]
print "例如:" +"./test.py IP.txt"
sys.exit()
def getbanner(IP,port):
socket.setdefaulttimeout(2)
s=socket.socket()
try:
s.connect((IP,port))
banner=s.recv(1024)
s.close()
return banner
except:
pass
def checkbanner(IP,port):
banner=getbanner(IP,port)
if banner:
if ("2.3.4" in banner):
print IP + " it is valubale",
else:
print IP + " it is not valuable",
else:
print IP + " it not have banner"
if __name__=="__main__":
filename=str(sys.argv[1].strip())
if not os.path.exists(filename):
print "请重新输入"
sys.exit()
port=21
filename=str(sys.argv[1].strip())
f=open(filename,'r')
for i in f.readlines():
IP=i.strip("\n")
t=Thread(target=checkbanner,args=(IP,port))
t.start()
f.close()