ObjectTelnet.py 6.6 KB

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