ObjectSsh.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import types
  2. import paramiko
  3. import sys
  4. import time
  5. from FiberHome import FiberHome
  6. from Furukawa import Furukawa
  7. from Huawei import Huawei
  8. from ZTE import ZTE
  9. from OLTBase import OLTBase
  10. from ObjectConnection import ObjectConnection
  11. class ObjectSsh(ObjectConnection):
  12. def __init__(self):
  13. ObjectConnection.__init__(self)
  14. self.stdin = None
  15. self.stdout = None
  16. def connect(self):
  17. """
  18. Connect by ssh to olt
  19. """
  20. if self.brand.upper() == ZTE.__name__.upper():
  21. self.olt = ZTE(self.model, False)
  22. elif self.brand.upper() == FiberHome.__name__.upper():
  23. self.olt = FiberHome(self.model, False)
  24. elif self.brand.upper() == Furukawa.__name__.upper():
  25. self.olt = Furukawa(self.model, False)
  26. elif self.brand.upper() == Huawei.__name__.upper():
  27. self.olt = Huawei(self.model, False)
  28. else:
  29. self.olt = OLTBase(None, False)
  30. ssh = paramiko.SSHClient()
  31. ssh.load_system_host_keys()
  32. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  33. ssh.connect(hostname=self.hostname, port=self.port, username=self.user, password=self.password,
  34. allow_agent=False, look_for_keys=False)
  35. chan = ssh.invoke_shell()
  36. self.stdin = chan.makefile('wb')
  37. self.stdout = chan.makefile('r')
  38. # wait to olt to initialice terminal
  39. self.read_data(self.olt.get_expected_initial())
  40. if self.file_name is not None:
  41. self.connection_file()
  42. elif self.data is not None:
  43. self.connection_data()
  44. else:
  45. self.connection_old()
  46. while self.QUIT:
  47. # send quit to terminal until exit confirmation
  48. self.command_quit(self.olt.get_write_exit())
  49. self.stdin.close()
  50. self.stdout.close()
  51. ssh.close()
  52. self.save_log()
  53. exit(self.RUN_OK)
  54. def command_enable(self, command):
  55. """
  56. Check's if the command is enable and execute then
  57. :param command:
  58. :return: Return True if enable otherwise False
  59. """
  60. if command.lower() == self.olt.get_write_enable().lower():
  61. if self.olt.run_enable():
  62. self.stdin.write(self.olt.get_write_enable() + self.NEW_LINE_UNIX)
  63. if self.olt.run_enable_password():
  64. self.read_data(self.olt.get_expected_enable_password())
  65. self.stdin.write(self.password_enable + self.NEW_LINE_UNIX)
  66. self.read_data(self.olt.get_expected_cardinal())
  67. return True
  68. return False
  69. def command_quit(self, command):
  70. """
  71. Check's if the command is quit and execute then
  72. :param command:
  73. :return: Return True if enable otherwise False
  74. """
  75. if command.lower() == self.olt.get_write_exit().lower():
  76. self.stdin.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
  77. position = self.read_data([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()])
  78. if position == 2:
  79. # quit terminal
  80. self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  81. self.stdin.flush()
  82. self.QUIT = False
  83. return True
  84. return False
  85. def command_execute(self, command, expected):
  86. """
  87. Check's the command
  88. :param command:
  89. :param expected: Expected string to stop listening
  90. """
  91. self.command_print(command)
  92. if not self.command_enable(command) and not self.command_quit(command):
  93. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  94. if command.__len__() > 0:
  95. self.stdin.write(command + self.NEW_LINE_UNIX)
  96. self.stdin.flush()
  97. self.read_data(expected)
  98. def read_data(self, character):
  99. """
  100. Read channel waiting parameter character
  101. :param character: The character
  102. """
  103. buffer_size = 1024
  104. all_data = ""
  105. stop = False
  106. position = 1
  107. if isinstance(character, types.StringType):
  108. character = [character]
  109. while not self.stdout.channel.exit_status_ready() and not stop:
  110. all_data += self.stdout.channel.recv(buffer_size)
  111. while self.stdout.channel.recv_ready():
  112. all_data += self.stdout.channel.recv(buffer_size)
  113. nc = 1
  114. for ch in character:
  115. if ch in all_data:
  116. stop = True
  117. position = nc
  118. break
  119. nc += 1
  120. print(str(all_data))
  121. self.all_data = self.all_data + str(all_data)
  122. return position
  123. def connection_old(self):
  124. """
  125. Old method
  126. """
  127. while 1:
  128. line = sys.stdin.readline()
  129. if not line:
  130. break
  131. self.command_execute(line, self.olt.get_expected_cardinal())
  132. time.sleep(1)
  133. if not self.QUIT:
  134. return
  135. def connection_data(self):
  136. """
  137. Recive data separeted by ;
  138. """
  139. # $(sed ':a;N;$!ba;s/\n/;/g' file)
  140. content = self.data.split(";")
  141. for line in content:
  142. self.command_execute(line, self.olt.get_expected_cardinal())
  143. if not self.QUIT:
  144. return
  145. def connection_file(self):
  146. """
  147. Execute command from file
  148. """
  149. with open(self.file_name) as f:
  150. content = f.readlines()
  151. for line in content:
  152. self.command_execute(line, self.olt.get_expected_cardinal())
  153. if not self.QUIT:
  154. return