ObjectSsh.py 7.5 KB

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