Written in python banner are two ways to get codes

1, no options and help information

# ! / Usr / bin / Python the env 
# Coding: UTF. 8- 

Import Socket
 Import SYS
 Import OS
 from Threading Import the Thread
 # Import module 
IF len (the sys.argv) = 2! :
     Print  " input mode: " , sysargv [0] , " IPliebiao " 
    Print  " example: ./test.py /root/ip.txt " 
    the sys.exit () 

DEF getBanner (IP, Port): 
    socket.setdefaulttimeout ( 2 )
 # set the timeout time is two seconds 
    S = Socket. socket ()
 #Method calls the socket module socket in 
    the try : 
        s.connect ((IP, Port)) 
    # receiving IP and port 
        Result = s.recv (1024 )
     # receiving 1024b return to the previous data information 
        S.CLOSE ()
     # disconnect 
        return result
     # output 
    the except :
         Pass 

DEF checkVulns (IP, Port): 
    Banner = getBanner (IP, Port)
     IF Banner:
         IF ( " 2.0 "  in Banner):
             Print IP + " IS Vulnerable " 
        the else:
             Print IP + " IS unvulnerable " 
    the else :
         Print IP, " Not Banner GET " 
    # determination result of whether the server version 2.0 
DEF main ():
     # not as module 
    filename = STR (the sys.argv [. 1 ] .strip ())
     IF  not os.path.exists (filename):
         Print  " file does not exist " 
        the sys.exit () 
    # determines whether or not the introduction of the file exists, the absence of closed 
    F = Open (filename, " R & lt " )
     for I in f.readlines () : 
        ipi.strip = ( " \ n- " ) 
        Port = 22 is 
        T = the Thread (target = checkVulns, args = (IP, Port)) 
        t.start () 
    f.close () 
    # import documents, and removing breaks 

IF  the __name__ = = " __main__ " : 
    main () 
    # output

 

2, there are options and help information

#!/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()
    #Setting options -f and -i and help -h 
    
    filename = options.filename 
    address = options.address 

    IF (filename == None and address == None):
         Print  " Please specify the IP address or IP list file " 
        sys.exit ( ) 
        # judged that the input is not empty 
    IF filename:
         IF  not os.path.exists (filename):
             Print  " bucunzai " 
            the sys.exit () 
        F = Open (filename, " R & lt " )
         for I in f.readlines (): 
            IP=i.strip("\n")
            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 inRange (1,255 ): 
            IP = prefix + STR (I) 
            Port = 22 is 
            T = the Thread (target = checkVulns, args = (IP, Port)) 
            t.start () 
    # arrangement introduced -i parameter 

IF  the __name__ == " __main__ " : 
    main ()

 

Guess you like

Origin www.cnblogs.com/liujizhou/p/11924965.html