ObjectSsh.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  117. if not self.command_enable(command) and not self.command_quit(command):
  118. if command.__len__() > 0:
  119. if self.debug:
  120. print "Send: "
  121. print command
  122. self.stdin.write(command + self.NEW_LINE_UNIX)
  123. self.stdin.flush()
  124. self.read_data(expected)
  125. def read_data(self, character):
  126. """
  127. Read channel waiting parameter character
  128. :param character: The character
  129. """
  130. buffer_size = 1024
  131. all_data = ""
  132. stop = False
  133. position = 1
  134. if isinstance(character, types.StringType):
  135. character = [character]
  136. if self.debug:
  137. print "Expected: "
  138. print character
  139. print "Recived:"
  140. while not self.stdout.channel.exit_status_ready() and not stop:
  141. all_data += self.stdout.channel.recv(buffer_size)
  142. if self.debug:
  143. print all_data
  144. while self.stdout.channel.recv_ready():
  145. all_data += self.stdout.channel.recv(buffer_size)
  146. if self.debug:
  147. print all_data
  148. nc = 1
  149. for ch in character:
  150. if ch is not None and ch in all_data:
  151. stop = True
  152. position = nc
  153. break
  154. nc += 1
  155. print(str(all_data))
  156. self.all_data = self.all_data + str(all_data)
  157. return position
  158. def connection_old(self):
  159. """
  160. Old method
  161. """
  162. while 1:
  163. line = sys.stdin.readline()
  164. if not line:
  165. break
  166. self.command_execute(line, self.olt.get_expected_cardinal())
  167. time.sleep(1)
  168. if not self.QUIT:
  169. return
  170. def connection_data(self):
  171. """
  172. Recive data separeted by ;
  173. """
  174. # $(sed ':a;N;$!ba;s/\n/;/g' file)
  175. content = self.data.split(";")
  176. for line in content:
  177. self.command_execute(line, self.olt.get_expected_cardinal())
  178. if not self.QUIT:
  179. return
  180. def connection_file(self):
  181. """
  182. Execute command from file
  183. """
  184. with open(self.file_name) as f:
  185. content = f.readlines()
  186. for line in content:
  187. self.command_execute(line, self.olt.get_expected_cardinal())
  188. if not self.QUIT:
  189. return