################################################################ # Runs multiple commands against IOS device # in global exec mode only # # Now works with non tacacs enabled devices, # also able to deal with unknown hosts # # Author: Gideon Street # Date: November 2006 # # Version 0.3 - Still needs error checking built in somewhere # for username and passwords # # Disclaimer: # Whilst this currently does what I need it to, there's # a chance it could destroy your network # Use at your own risk, no liability if it does break your stuff # # ################################################################# import getpass import sys import telnetlib import time import os user = raw_input("Enter your username: ") password = getpass.getpass("Enter telnet password") enablepass = getpass.getpass("Enter enable password") print "\n" print "Enter commands one line at a time" print "hit return after each command, " print "leave a blank line and hit enter to start gathering information." print "\n" list = [] #create place to store the entered commands cmd = raw_input("What command/s do you want to run? ") while cmd != "": list.append(cmd) #put the commands into the list cmd = raw_input("What command/s do you want to run? ") HostFile = open("hosts.txt", 'r') #this grabs the hosts from the hosts.txt file for item in (x.strip() for x in HostFile): #strips newline character from device name try: tn = telnetlib.Telnet(item) #telnets to each device in file print "\n" print "Connecting to device",item logcheck = tn.expect (["Username: "]) #,"Password: ", ">"]) if logcheck[0] == 0: # checks for Username prompt tn.write(user + "\n") #write username and enter tn.read_until("Password: ") tn.write(password + "\n") tn.read_until(">") #wait for prompt tn.write("terminal length 0" + "\n") ##set term length so dont have to page tn.read_until(">") tn.write("enable" + "\n") #enable mode tn.read_until("Password: ") tn.write(enablepass + "\n") tn.read_until("#") #wait for prompt if os.path.isfile(item + ".log"): #checks if log file exists for device outp = open(item + ".log","a") #opens file with append only rights else: outp = open(item + ".log","w") #otherwise create file for commands in list: tn.write(commands + '\n') promptCheck = tn.read_until('#') #wait for prompt and store output outp.write(promptCheck) #write command output to logfile print commands + " - Completed" #play nicely time.sleep(1) tn.write('quit' + '\n') #quits from telnet sessions outp.close() #close log file print "connection to",item + " closed" #play nicely with user print "\n" else: # if no username prompt print "uggh, no TACACS \n" print "added hostname to NO-TACACS.txt, we'll deal with these later" print "moving on" if os.path.isfile("NO-TACACS.log"): #checks if log file exists for device outerror = open("NO-TACACS.log","a") #opens file with append only rights else: outerror = open("NO-TACACS.log","w") #otherwise create file outerror.write(item +'\n') #write host name to no tacacs log file outerror.close() #close no tacacs log file tn.close() except: if os.path.isfile("Unknown Hosts.log"): nohost = open("Unknown Hosts.log","a") else: nohost = open("Unknown Hosts.log","w") print "\n" print "Host unknown or not responding, check 'Unknown Hosts.log' file for name/address" nohost.write(item +"\n") nohost.close() continue tn.close() if os.path.isfile("NO-TACACS.log"): try: print "finished all devices with tacacs, now onto the no tacacs devices" print "\n" telnetpassword = raw_input("What's the telnet password? ") enablesecret = raw_input("So what's the enable secret? ") HostFile = open("NO-TACACS.log", 'r') #this grabs the hosts from the hosts.txt file for item in (x.strip() for x in HostFile): #strips newline character from device name tn = telnetlib.Telnet(item) #telnets to each device in file print "\n" print "Connecting to device",item logcheck = tn.expect (["Password: ", ">"]) if logcheck[0] == 0: # telnet password tn.write(telnetpassword + "\n") #write telnet password and enter tn.read_until(">") #wait for prompt tn.write("terminal length 0" + "\n") ##set term length so dont have to page tn.read_until(">") tn.write("enable" + "\n") #enable mode tn.read_until("Password: ") tn.write(enablesecret + "\n") tn.read_until("#") #wait for prompt if os.path.isfile(item + ".log"): #checks if log file exists for device outp = open(item + ".log","a") #opens file with append only rights else: outp = open(item + ".log","w") #otherwise create file for commands in list: tn.write(commands + '\n') promptCheck = tn.read_until('#') #wait for prompt and store output outp.write(promptCheck) #write command output to logfile print commands + " - Completed" #play nicely time.sleep(1) tn.write('quit' + '\n') #quits from telnet sessions outp.close() #close log file tn.close() print "connection to",item + " closed" #play nicely with user print "\n" else: # if no telnet password tn.write("terminal length 0" + "\n") ##set term length so dont have to page tn.read_until(">") tn.write("enable" + "\n") #enable mode tn.read_until("Password: ") tn.write(enablesecret + "\n") tn.read_until("#") #wait for prompt if os.path.isfile(item + ".log"): #checks if log file exists for device outp = open(item + ".log","a") #opens file with append only rights else: outp = open(item + ".log","w") #otherwise create file for commands in list: tn.write(commands + '\n') promptCheck = tn.read_until('#') #wait for prompt and store output outp.write(promptCheck) #write command output to logfile print commands + " - Completed" #play nicely time.sleep(1) tn.write('quit' + '\n') #quits from telnet sessions outp.close() #close log file tn.close() print "connection to",item + " closed" #play nicely with user print "\n" except: # picks up unknown device names end tn.close() print "all done, go outside and play"