ObjectSsh.py 6.8 KB

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