ObjectSsh.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. cardinal = self.olt.get_expected_cardinal()
  95. if isinstance(cardinal, list):
  96. read_data_list = cardinal + [self.olt.get_expected_exit()]
  97. else:
  98. read_data_list = [cardinal, self.olt.get_expected_exit()]
  99. position = self.read_data(read_data_list)
  100. if position == 2:
  101. # quit terminal
  102. if self.debug:
  103. print "Send: "
  104. print self.olt.get_write_exit_confirmation()
  105. self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  106. self.stdin.flush()
  107. self.QUIT = False
  108. # except self.tn.error, e:
  109. except IOError, e:
  110. if e.errno == errno.EPIPE:
  111. self.QUIT = False
  112. return True
  113. return False
  114. def command_execute(self, command, expected):
  115. """
  116. Check's the command
  117. :param command:
  118. :param expected: Expected string to stop listening
  119. """
  120. self.command_print(command)
  121. command = command.strip()
  122. if "\\n" in command and (len(command) == 4 or len(command) == 2):
  123. print "Sending ENTER \\r\\n"
  124. command = '\r\n'
  125. else:
  126. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  127. if not self.command_enable(command) and not self.command_quit(command):
  128. if command.__len__() > 0:
  129. if self.debug:
  130. print "Send: "
  131. print command
  132. self.stdin.write(command + self.NEW_LINE_UNIX)
  133. self.stdin.flush()
  134. self.read_data(expected)
  135. def read_data(self, character):
  136. """
  137. Read channel waiting parameter character
  138. :param character: The character
  139. """
  140. buffer_size = 1024
  141. all_data = ""
  142. stop = False
  143. position = 1
  144. if isinstance(character, types.StringType):
  145. character = [character]
  146. if self.debug:
  147. print "Expected: "
  148. print character
  149. print "Received: "
  150. while not self.stdout.channel.exit_status_ready() and not stop:
  151. all_data += self.stdout.channel.recv(buffer_size)
  152. if self.debug:
  153. print "Debug: " + all_data
  154. while self.stdout.channel.recv_ready():
  155. all_data += self.stdout.channel.recv(buffer_size)
  156. if self.debug:
  157. print "Debug: " + all_data
  158. nc = 1
  159. for ch in character:
  160. if ch is not None and ch in all_data:
  161. stop = True
  162. position = nc
  163. break
  164. nc += 1
  165. print str(all_data)
  166. self.all_data = self.all_data + str(all_data)
  167. return position
  168. def connection_old(self):
  169. """
  170. Old method
  171. """
  172. while 1:
  173. line = sys.stdin.readline()
  174. if not line:
  175. break
  176. self.command_execute(line, self.olt.get_expected_cardinal())
  177. time.sleep(1)
  178. if not self.QUIT:
  179. return
  180. def connection_data(self):
  181. """
  182. Recive data separeted by ;
  183. """
  184. # $(sed ':a;N;$!ba;s/\n/;/g' file)
  185. content = self.data.split(";")
  186. for line in content:
  187. self.command_execute(line, self.olt.get_expected_cardinal())
  188. if not self.QUIT:
  189. return
  190. def connection_file(self):
  191. """
  192. Execute command from file
  193. """
  194. with open(self.file_name) as f:
  195. content = f.readlines()
  196. for line in content:
  197. self.command_execute(line, self.olt.get_expected_cardinal())
  198. if not self.QUIT:
  199. return