ObjectTelnet.py 8.0 KB

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