ObjectSsh.py 6.0 KB

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