ObjectSsh.py 7.5 KB

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