ObjectTelnet.py 6.9 KB

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