ObjectTelnet.py 5.8 KB

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