Guest User

Untitled

a guest
Sep 19th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import telnetlib
  3. import time
  4. import argparse
  5. import sys
  6.  
  7. class MyTelConn(object):
  8. connection_timeout = 5
  9. reading_timeout = 5
  10. def __init__(self, user, passwd, host, port=23):
  11. self.user = user
  12. self.passwd = passwd
  13. self.host = host
  14. self.port = port
  15. self.connection = None
  16. self.login_prompt = b"Username:"
  17. self.password_prompt = b"Password:"
  18. self.command_file = False
  19. self._file = None
  20.  
  21. @property
  22. def file(self):
  23. return self._file
  24.  
  25. @file.setter
  26. def file(self, file):
  27. self.command_file = True
  28. self._file = file
  29.  
  30. def connect(self):
  31. try:
  32. #====> Login to the Device
  33. self.connection = telnetlib.Telnet(self.host, self.port, self.connection_timeout)
  34. self.connection.read_until(self.login_prompt, self.reading_timeout)
  35. self.connection.write(self.user.encode('ascii') + b"\n")
  36. time.sleep(1)
  37.  
  38. #=====> Setup Terminal Length for get the entire output
  39. self.connection.read_until(self.password_prompt, self.reading_timeout)
  40. self.connection.write(self.passwd.encode('ascii') + b"\n")
  41. time.sleep(1)
  42.  
  43. print("successfully login to device {} host !".format(self.host))
  44.  
  45. except IOError as e:
  46. return "Input parameter error {}, Please check username, passwd and file name".format(e)
  47.  
  48. def send_config_manual(self, code):
  49. try:
  50. # ========> Enter to Global Config
  51. self.connection.write(b"\n")
  52. self.connection.write("config t\n".encode('ascii'))
  53. time.sleep(1)
  54.  
  55. self.connection.write(code.encode('ascii') + b'\n')
  56. time.sleep(1)
  57.  
  58. print("successfully send command code <{}> to device {} !".format(code, self.host))
  59.  
  60. except IOError as e:
  61. return "Input parameter error {}".format(e)
  62.  
  63. finally:
  64. output = self.connection.read_very_eager()
  65. print(output)
  66. self.connection.close()
  67.  
  68.  
  69. def send_show_code(self, code):
  70. try:
  71. self.connection.write('term mon\n'.encode('ascii'))
  72. time.sleep(1)
  73.  
  74. self.connection.write(code.encode('ascii') + b'\n')
  75. time.sleep(1)
  76.  
  77. print("successfully send show command <{}> to device! {} ".format(code, self.host))
  78.  
  79. except IOError as e:
  80. return "Input parameter error {}".format(e)
  81.  
  82. finally:
  83. output = self.connection.read_very_eager()
  84. print(output)
  85. self.connection.close()
  86.  
  87.  
  88. def send_config_file(self):
  89. try:
  90. # ========> Enter to Global Config
  91. self.connection.write(b"\n")
  92. self.connection.write("config t\n".encode('ascii'))
  93. time.sleep(1)
  94.  
  95. with open(self.file, 'r') as f_command:
  96. for line in f_command.readlines():
  97. self.connection.write(line.encode('ascii') + b'\n')
  98. time.sleep(1)
  99.  
  100. print("successfully send file configuration <{}> to device {} !".format(self.file, self.host))
  101.  
  102. except IOError:
  103. return "invalid File Access!.."
  104.  
  105. finally:
  106. output = self.connection.read_very_eager().decode(encoding='utf-8#')
  107. print(output)
  108. self.connection.close()
  109.  
  110.  
  111. def main():
  112. if len(sys.argv) <= 1:
  113. print("Use {} --h or --help to get help menu".format(sys.argv[0]))
  114.  
  115. else:
  116.  
  117. parser = argparse.ArgumentParser(description="||======= REMOTE TELNET MENU =======>\n{} <=====||".format(sys.argv[0]))
  118. parser.add_argument('--ip', help="Ipv4 address eg. 192.168.1.1")
  119. parser.add_argument('--u', help="username or login account ")
  120. parser.add_argument('--p', help="password")
  121. parser.add_argument('--send', help='''<show> send show command,
  122. <sm> send configuration manually,
  123. <sf> send configuration from file
  124. ''',choices = ['show', 'sm', 'sf'])
  125.  
  126. args = parser.parse_args()
  127.  
  128. conn = MyTelConn(args.u, args.p, args.ip)
  129. conn.connect()
  130. if args.send == 'sf':
  131. import_file = input("Enter Your file command: ")
  132. conn.file = import_file
  133. conn.send_config_file()
  134.  
  135. elif args.send == 'show':
  136. show_command = input("Enter Your Show Command\n>: ")
  137. conn.send_show_code(show_command)
  138.  
  139. elif args.send == 'sm':
  140. manual_command = input("Enter your Command\n>: ")
  141. conn.send_config_manual(manual_command)
  142.  
  143. if __name__ == '__main__':
  144. main()
Add Comment
Please, Sign In to add comment