ObjectTelnet.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import errno
  2. import sys
  3. import telnetlib
  4. import time
  5. import fcntl
  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 ObjectTelnet(ObjectConnection):
  13. def __init__(self):
  14. ObjectConnection.__init__(self)
  15. self.tn = None
  16. self.debug = False
  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. print("Telnet client ...\n")
  32. f = open("/var/lock/" + self.hostname, "w")
  33. print("Open lock file\n")
  34. fcntl.flock(f, fcntl.LOCK_EX)
  35. print("Lock Acquire\n")
  36. print("Login in...\n")
  37. self.tn = telnetlib.Telnet(self.hostname, self.port)
  38. if self.debug:
  39. self.tn.set_debuglevel(1)
  40. error = self.read_data([self.olt.get_expected_name()])
  41. if error != -1:
  42. self.tn.write(self.user + self.NEW_LINE_UNIX)
  43. error = self.read_data([self.olt.get_expected_password()])
  44. if error != -1:
  45. self.tn.write(self.password + self.NEW_LINE_UNIX)
  46. print("Logged in...\n")
  47. print self.tn.read_very_lazy()
  48. cardinal = self.olt.get_expected_cardinal()
  49. if isinstance(cardinal, list):
  50. read_data_list = cardinal
  51. else:
  52. read_data_list = [cardinal]
  53. error = self.read_data(read_data_list)
  54. if error != -1:
  55. if self.file_name is not None:
  56. self.connection_file()
  57. elif self.data is not None:
  58. self.connection_data()
  59. else:
  60. self.connection_old()
  61. print("-----FIN-----")
  62. nc = 0
  63. while self.QUIT:
  64. if nc == 20:
  65. self.QUIT = False
  66. # send quit to terminal until exit confirmation
  67. self.command_quit(self.olt.get_write_exit())
  68. nc += 1
  69. self.tn.close()
  70. self.save_log()
  71. exit(self.RUN_OK)
  72. exit(self.RUN_ERROR)
  73. def connection_old(self):
  74. """
  75. Old method
  76. """
  77. while 1:
  78. line = sys.stdin.readline()
  79. if not line:
  80. break
  81. print(self.tn.read_until(self.olt.get_expected_cardinal()))
  82. time.sleep(1)
  83. def connection_data(self):
  84. """
  85. Recive data separeted by ;
  86. """
  87. # $(sed ':a;N;$!ba;s/\n/;/g' file)
  88. content = self.data.split(";")
  89. for line in content:
  90. self.command_execute(line, self.olt.get_expected_cardinal())
  91. def connection_file(self):
  92. """
  93. Execute command from file
  94. """
  95. with open(self.file_name) as f:
  96. content = f.readlines()
  97. for line in content:
  98. self.command_execute(line, self.olt.get_expected_cardinal())
  99. def command_enable(self, command):
  100. """
  101. Check's if the command is enable and execute then
  102. :param command:
  103. :return: Return True if enable otherwise False
  104. """
  105. if command.lower() == self.olt.get_write_enable().lower():
  106. if self.olt.run_enable():
  107. self.tn.write(self.olt.get_write_enable() + self.NEW_LINE_UNIX)
  108. if self.olt.run_enable_password():
  109. self.read_data([self.olt.get_expected_enable_password()])
  110. self.tn.write(self.password_enable + self.NEW_LINE_UNIX)
  111. cardinal = self.olt.get_expected_cardinal()
  112. if isinstance(cardinal, list):
  113. read_data_list = cardinal
  114. else:
  115. read_data_list = [cardinal]
  116. self.read_data(read_data_list)
  117. return True
  118. return False
  119. def command_quit(self, command):
  120. """
  121. Check's if the command is quit and execute then
  122. :param command:
  123. :return: Return True if enable otherwise False
  124. """
  125. if command.lower() == self.olt.get_write_exit().lower():
  126. try:
  127. self.tn.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX)
  128. read_data = None
  129. with_confirmation = False
  130. if self.olt.get_expected_exit() is not None:
  131. cardinal = self.olt.get_expected_cardinal()
  132. if isinstance(cardinal, list):
  133. read_data = cardinal + [self.olt.get_expected_exit()]
  134. else:
  135. read_data = [cardinal, self.olt.get_expected_exit()]
  136. with_confirmation = True
  137. if self.olt.get_expected_exit_with_confirmation() is not None:
  138. cardinal = self.olt.get_expected_cardinal()
  139. if isinstance(cardinal, list):
  140. read_data = cardinal + [self.olt.get_expected_exit_with_confirmation()]
  141. else:
  142. read_data = [cardinal, self.olt.get_expected_exit_with_confirmation()]
  143. with_confirmation = False
  144. if read_data is not None:
  145. position = self.read_data(read_data)
  146. if position == 2:
  147. # quit terminal
  148. if with_confirmation:
  149. self.tn.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  150. self.QUIT = False
  151. # except self.tn.error, e:
  152. except IOError, e:
  153. if e.errno == errno.EPIPE:
  154. self.QUIT = False
  155. return True
  156. return False
  157. def command_execute(self, command, expected):
  158. """
  159. Check's the command
  160. :param command:
  161. :param expected: Expected string to stop listening
  162. """
  163. self.command_print(command)
  164. command = command.strip()
  165. if "\\n" in command and (len(command) == 4 or len(command) == 2):
  166. print "Sending ENTER \\r\\n"
  167. command = '\r\n'
  168. else:
  169. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  170. if not self.command_enable(command) and not self.command_quit(command):
  171. if command.__len__() > 0:
  172. if self.debug:
  173. print "Send: "
  174. print command
  175. self.tn.write(command + self.NEW_LINE_UNIX)
  176. self.read_data(expected)
  177. def read_data(self, expected):
  178. """
  179. Read channel waiting parameter character
  180. :param expected: List of expected string
  181. """
  182. iteration = 0
  183. position = -1
  184. while iteration < 6:
  185. (i, obj, all_data) = self.tn.expect(expected, 10)
  186. nc = 1
  187. for ch in expected:
  188. if ch in all_data:
  189. position = nc
  190. break
  191. nc += 1
  192. if position == -1:
  193. iteration += 1
  194. else:
  195. iteration = 6
  196. print(str(all_data))
  197. self.all_data = self.all_data + str(all_data)
  198. return position