ObjectTelnet.py 8.2 KB

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