Guest User

Untitled

a guest
Dec 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #This is the telnet Library for Python3
  2. #Make sure transport input telnet is enabled on router validate
  3. #This scripts will run commands you choose on the remote devices using telnet as transport
  4. #The results will be print to screen and captured in files named ("router_" + HOST)
  5. #(c) 2017 Todd Riemenschneider
  6.  
  7. from getpass import getpass
  8. import telnetlib
  9.  
  10. user = input("Username: ")
  11. password = getpass("Password: ")
  12. enpw = getpass("Enable: ")
  13.  
  14. with open('host_file.txt') as f:
  15. hosts = f.read().splitlines()
  16.  
  17. commands = '''
  18. show cdp neighbor
  19. show run
  20. show version
  21. show inventory
  22. '''
  23.  
  24. cmds = commands.splitlines()
  25.  
  26.  
  27. for HOST in hosts:
  28. print("Connecting to host:", HOST)
  29. tn = telnetlib.Telnet(HOST)
  30. tn.read_until(b"Username: ")
  31. tn.write(user.encode('ascii') + b"\n")
  32. if password:
  33. tn.read_until(b"Password: ")
  34. tn.write(password.encode('ascii') + b"\n")
  35. tn.write(b"enable \n")
  36. tn.read_until(b"Password: ")
  37. tn.write(enpw.encode('ascii') + b"\n")
  38.  
  39. tn.write(b"term length 0\n")
  40. #iterate through cmds using for loop
  41. for CMD in cmds:
  42. tn.write(CMD.encode('ascii') + b"\n")
  43. tn.write(b"exit\n")
  44. readoutput = tn.read_all().decode('ascii')
  45. saveoutput = open("router_" + HOST, "w")
  46. saveoutput.write(readoutput)
  47. saveoutput.write("\n")
  48. saveoutput.close
  49. #This command will print out the saved output on your screen
  50. print(readoutput)
Add Comment
Please, Sign In to add comment