ObjectTelnet.py 8.2 KB

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