ObjectTelnet.py 6.7 KB

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