python---抓取banner信息升级版--使用多线程

创建多个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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值