Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #ex) $ ix_uptime.py 192.168.0.1 username password
  3. import getpass
  4. from telnetlib import Telnet
  5. from sys import argv
  6.  
  7. header = "IX2015# "
  8. command = "show uptime\r"
  9. #HOST = input("Enter your IX\'s IP addr: ")
  10. #user = input("Enter your remote account: ").encode('ascii')
  11. #password = getpass.getpass().encode("ascii")
  12. HOST = argv[1]
  13. user = argv[2].encode('ascii')
  14. password = argv[3].encode('ascii')
  15.  
  16. tn = Telnet()
  17. #block any option
  18. tn.set_option_negotiation_callback(lambda x,y,z:None)
  19. tn.open(HOST)
  20.  
  21. #login and cut welcome messages
  22. tn.read_until(b"login: ")
  23. tn.write(user + b"\r")
  24. if password:
  25. tn.read_until(b"Password: ")
  26. tn.write(password + b"\r")
  27. tn.read_until(header.encode("ascii"))
  28. tn.write("\r")
  29.  
  30. tn.write(command.encode("ascii"))
  31. tn.write(b"exit\r")
  32. raw_msg = tn.read_all().decode('ascii')
  33.  
  34. #delete header
  35. def check_header(txt):
  36. if txt.find(header) == -1:
  37. return txt.replace("\r", "\n")
  38. else:
  39. return ''
  40.  
  41. clear_msg = ''.join(list(map(check_header, raw_msg.split('\n'))))
  42. print(clear_msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement