ObjectSsh.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. def connect(self):
  18. """
  19. Connect by ssh to olt
  20. """
  21. if self.brand.upper() == ZTE.__name__.upper():
  22. self.olt = ZTE(self.model, False)
  23. elif self.brand.upper() == FiberHome.__name__.upper():
  24. self.olt = FiberHome(self.model, False)
  25. elif self.brand.upper() == Furukawa.__name__.upper():
  26. self.olt = Furukawa(self.model, False)
  27. elif self.brand.upper() == Huawei.__name__.upper():
  28. self.olt = Huawei(self.model, False)
  29. else:
  30. self.olt = OLTBase(None, False)
  31. ssh = paramiko.SSHClient()
  32. ssh.load_system_host_keys()
  33. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  34. ssh.connect(hostname=self.hostname, port=self.port, username=self.user, password=self.password,
  35. allow_agent=False, look_for_keys=False)
  36. chan = ssh.invoke_shell()
  37. self.stdin = chan.makefile('wb')
  38. self.stdout = chan.makefile('r')
  39. # wait to olt to initialice terminal
  40. self.read_data(self.olt.get_expected_initial())
  41. if self.file_name is not None:
  42. self.connection_file()
  43. elif self.data is not None:
  44. self.connection_data()
  45. else:
  46. self.connection_old()
  47. while self.QUIT:
  48. # send quit to terminal until exit confirmation
  49. self.command_quit(self.olt.get_write_exit())
  50. self.stdin.close()
  51. self.stdout.close()
  52. ssh.close()
  53. self.save_log()
  54. exit(self.RUN_OK)
  55. def command_enable(self, command):
  56. """
  57. Check's if the command is enable and execute then
  58. :param command:
  59. :return: Return True if enable otherwise False
  60. """
  61. if command.lower() == self.olt.get_write_enable().lower():
  62. if self.olt.run_enable():
  63. self.stdin.write(self.olt.get_write_enable() + self.NEW_LINE_UNIX)
  64. if self.olt.run_enable_password():
  65. self.read_data(self.olt.get_expected_enable_password())
  66. self.stdin.write(self.password_enable + self.NEW_LINE_UNIX)
  67. self.read_data(self.olt.get_expected_cardinal())
  68. return True
  69. return False
  70. def command_quit(self, command):
  71. """
  72. Check's if the command is quit and execute then
  73. :param command:
  74. :return: Return True if enable otherwise False
  75. """
  76. if command.lower() == self.olt.get_write_exit().lower():
  77. try:
  78. self.stdin.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
  79. position = self.read_data([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()])
  80. if position == 2:
  81. # quit terminal
  82. self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  83. self.stdin.flush()
  84. self.QUIT = False
  85. # except self.tn.error, e:
  86. except IOError, e:
  87. if e.errno == errno.EPIPE:
  88. self.QUIT = False
  89. return True
  90. return False
  91. def command_execute(self, command, expected):
  92. """
  93. Check's the command
  94. :param command:
  95. :param expected: Expected string to stop listening
  96. """
  97. self.command_print(command)
  98. if not self.command_enable(command) and not self.command_quit(command):
  99. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  100. if command.__len__() > 0:
  101. self.stdin.write(command + self.NEW_LINE_UNIX)
  102. self.stdin.flush()
  103. self.read_data(expected)
  104. def read_data(self, character):
  105. """
  106. Read channel waiting parameter character
  107. :param character: The character
  108. """
  109. buffer_size = 1024
  110. all_data = ""
  111. stop = False
  112. position = 1
  113. if isinstance(character, types.StringType):
  114. character = [character]
  115. while not self.stdout.channel.exit_status_ready() and not stop:
  116. all_data += self.stdout.channel.recv(buffer_size)
  117. while self.stdout.channel.recv_ready():
  118. all_data += self.stdout.channel.recv(buffer_size)
  119. nc = 1
  120. for ch in character:
  121. if ch in all_data:
  122. stop = True
  123. position = nc
  124. break
  125. nc += 1
  126. print(str(all_data))
  127. self.all_data = self.all_data + str(all_data)
  128. return position
  129. def connection_old(self):
  130. """
  131. Old method
  132. """
  133. while 1:
  134. line = sys.stdin.readline()
  135. if not line:
  136. break
  137. self.command_execute(line, self.olt.get_expected_cardinal())
  138. time.sleep(1)
  139. if not self.QUIT:
  140. return
  141. def connection_data(self):
  142. """
  143. Recive data separeted by ;
  144. """
  145. # $(sed ':a;N;$!ba;s/\n/;/g' file)
  146. content = self.data.split(";")
  147. for line in content:
  148. self.command_execute(line, self.olt.get_expected_cardinal())
  149. if not self.QUIT:
  150. return
  151. def connection_file(self):
  152. """
  153. Execute command from file
  154. """
  155. with open(self.file_name) as f:
  156. content = f.readlines()
  157. for line in content:
  158. self.command_execute(line, self.olt.get_expected_cardinal())
  159. if not self.QUIT:
  160. return