ObjectTelnet.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if self.olt.get_expected_exit() is not None:
  107. # data = self.tn.read_all()
  108. position = self.read_data([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()])
  109. if position == 2:
  110. # quit terminal
  111. self.tn.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  112. self.QUIT = False
  113. # (i, obj, data) = self.tn.expect([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()], 10)
  114. # if self.olt.get_expected_exit() in data:
  115. # # quit terminal
  116. # self.tn.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX)
  117. # self.QUIT = False
  118. return True
  119. return False
  120. def command_execute(self, command, expected):
  121. """
  122. Check's the command
  123. :param command:
  124. :param expected: Expected string to stop listening
  125. """
  126. command = command.strip()
  127. if not (command[:-1] == self.NEW_LINE_UNIX or command[:-2] == self.NEW_LINE_WINDOWS):
  128. self.command_print(command)
  129. if not self.command_enable(command) and not self.command_quit(command):
  130. command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX)
  131. if command.__len__() > 0:
  132. self.tn.write(command + self.NEW_LINE_UNIX)
  133. self.read_data([expected])
  134. def read_data(self, expected):
  135. """
  136. Read channel waiting parameter character
  137. :param expected: List of expected string
  138. """
  139. position = 1
  140. (i, obj, all_data) = self.tn.expect(expected, 10)
  141. nc = 1
  142. for ch in expected:
  143. if ch in all_data:
  144. position = nc
  145. break
  146. nc += 1
  147. print(str(all_data))
  148. self.all_data = self.all_data + str(all_data)
  149. return position