ObjectSsh.py 7.0 KB

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