ObjectTelnet.py 6.6 KB

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