import string import telnetlib import time import getopt,sys def usage(): #print "###############################################################" print print "Cisco config retriever by Rodrigo Marcos" print "www.secforce.co.uk" print print "usage():" print print "python cisco_config_retrieve -f -p -e " print print " -f: File with the Cisco routers, one per line" print " -p: Password (use '' for blank password)" print " -e: Enable password (use '' for blank password)" print " -h: Help page" print try: opts, args = getopt.getopt(sys.argv[1:], "hf:p:e:", ["help"]) except getopt.GetoptError: usage() sys.exit(2) try: for o, value in opts: if o in ("-h", "--help"): usage() sys.exit() if o == "-f": routers = value if o == "-p": password = value if o == "-e": en_password = value except: usage() sys.exit(2) print print "Cisco config retriever by Rodrigo Marcos" print "www.secforce.co.uk" file = open(routers, 'r') for router in file: print 'Connecting to ' + router + '...' host = router.strip() tn = telnetlib.Telnet(host) tn.read_until("Password: ") tn.write(password + "\n") prompt = tn.read_until('>') #We get the router's prompt prompt = prompt.strip() prompt = prompt[:-1] tn.write("en\n") # enable tn.read_until("Password: ") tn.write(en_password + "\n") # we enter password tn.read_until('#') tn.write('terminal length 0\n') # terminal length 0 tn.read_until(prompt + '#') print 'Retrieving config...' tn.write('show run\n') # execute show run output = tn.read_until(prompt + '#') print 'Disconecting' tn.close() outputfile = open('config_' + host + '.txt', 'w') outputfile.write(output) outputfile.close() print 'Config saved in ' + 'config_' + host + '.txt'