1、无选项和帮助信息
#!/usr/bin/env python #coding:utf-8 import socket import sys import os from threading import Thread #导入模块 if len(sys.argv) !=2: print "输入方式:",sysargv[0],"IPliebiao" print "例如: ./test.py /root/ip.txt" sys.exit() def getBanner(ip,port): socket.setdefaulttimeout(2) #设置超时时间为两秒 s=socket.socket() #调用socket模块中的socket方法 try: s.connect((ip,port)) #接收IP和端口 result=s.recv(1024) #接收返回信息中的前1024b数据 s.close() #断开连接 return result #输出结果 except: pass def checkVulns(ip,port): banner=getBanner(ip,port) if banner: if ("2.0" in banner): print ip+"is vulnerable" else: print ip+"is unvulnerable" else: print ip,"not get banner" #判断结果服务器是否为2.0版本 def main(): #不当做模块 filename=str(sys.argv[1].strip()) if not os.path.exists(filename): print "文件不存在" sys.exit() #判断引入文件是否存在,不存在则关闭 f=open(filename,"r") for i in f.readlines(): ip=i.strip(" ") port=22 t=Thread(target=checkVulns,args=(ip,port)) t.start() f.close() #引入文件,并去除分行符 if __name__=="__main__": main() #输出结果
2、有选项和帮助信息
#!/usr/bin/env python #coding:utf-8 import socket import sys import os from threading import Thread from optparse import OptionParser def getBanner(ip,port): socket.setdefaulttimeout(2) s=socket.socket() try: s.connect((ip,port)) result=s.recv(1024) s.close() return result except: pass def checkVulns(ip,port): banner=getBanner(ip,port) if banner: if ("2.0" in banner): print ip+"is vulnerable" else: print ip+"is unvulnerable" else: print ip,"not get banner" def main(): usage="Usage: %prog -f <filename> -i <ip address>" parser=OptionParser(usage=usage) parser.add_option("-f","--file",type="string",dest="filename",help="IP address file") parser.add_option("-i","--ip",type="string",dest="address",help="IP address") (options,args)=parser.parse_args() #设置选项-f和-i和帮助信息-h filename=options.filename address=options.address if (filename==None and address==None): print "请指定IP列表文件或IP地址" sys.exit() #判断输入不为空 if filename: if not os.path.exists(filename): print "bucunzai" sys.exit() f=open(filename,"r") for i in f.readlines(): ip=i.strip(" ") port=22 t=Thread(target=checkVulns,args=(ip,port)) t.start() f.close() #设置-f参数引入的方式 if address: prefix=address.split(".")[0]+"."+address.split(".")[1]+"."+address.split(".")[2]+"." for i in range(1,255): ip=prefix+str(i) port=22 t=Thread(target=checkVulns,args=(ip,port)) t.start() #设置-i参数引入的方式 if __name__=="__main__": main()