Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/usr/bin/env
  2. import telnetlib
  3. import time
  4. import socket
  5. import sys
  6.  
  7. TELNET_PORT = 23
  8. TELNET_TIMEOUT = 8
  9.  
  10. class Telnet(object):
  11.  
  12. def __init__(self, username, password, ip_addr, port):
  13. self.username = username
  14. self.password = password
  15. self.ip_addr = ip_addr
  16. self.port = port
  17. self.TIMEOUT = 8
  18.  
  19. def login(self, remote_conn):
  20. output = remote_conn.read_until('sername:', self.TIMEOUT)
  21. remote_conn.write(self.username + '\n')
  22. output += remote_conn.read_until('ssword:', self.TIMEOUT)
  23. remote_conn.write(self.password + '\n')
  24. return output
  25.  
  26. def send_command(self, remote_conn, command):
  27. command = command.rstrip()
  28. remote_conn.write(command + '\n')
  29. time.sleep(1)
  30. return remote_conn.read_very_eager()
  31.  
  32. @property
  33. def telnet_connect(self):
  34. try:
  35. return telnetlib.Telnet(self.ip_addr, self.port, self.TIMEOUT)
  36. except socket.timeout:
  37. sys.exit('connection timed-out')
  38.  
  39. def main():
  40.  
  41. router1 = Telnet('pyclass', '88newclass', '184.105.247.70', 23)
  42.  
  43. remote_conn = router1.telnet_connect
  44. output = router1.login(remote_conn)
  45.  
  46. time.sleep(1)
  47. output = remote_conn.read_very_eager()
  48.  
  49. output = router1.send_command(remote_conn, 'terminal length 0')
  50. output = router1.send_command(remote_conn, 'show version')
  51. output = output.strip('show version')
  52. output = output[:output.rfind('\n')]
  53. print(output)
  54.  
  55. remote_conn.close()
  56.  
  57. if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement