import types import errno import paramiko import sys import time from FiberHome import FiberHome from Furukawa import Furukawa from Huawei import Huawei from ZTE import ZTE from OLTBase import OLTBase from ObjectConnection import ObjectConnection class ObjectSsh(ObjectConnection): def __init__(self): ObjectConnection.__init__(self) self.stdin = None self.stdout = None def connect(self): """ Connect by ssh to olt """ if self.brand.upper() == ZTE.__name__.upper(): self.olt = ZTE(self.model, False) elif self.brand.upper() == FiberHome.__name__.upper(): self.olt = FiberHome(self.model, False) elif self.brand.upper() == Furukawa.__name__.upper(): self.olt = Furukawa(self.model, False) elif self.brand.upper() == Huawei.__name__.upper(): self.olt = Huawei(self.model, False) else: self.olt = OLTBase(None, False) ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=self.hostname, port=self.port, username=self.user, password=self.password, allow_agent=False, look_for_keys=False) chan = ssh.invoke_shell() self.stdin = chan.makefile('wb') self.stdout = chan.makefile('r') # wait to olt to initialice terminal self.read_data(self.olt.get_expected_initial()) if self.file_name is not None: self.connection_file() elif self.data is not None: self.connection_data() else: self.connection_old() while self.QUIT: # send quit to terminal until exit confirmation self.command_quit(self.olt.get_write_exit()) self.stdin.close() self.stdout.close() ssh.close() self.save_log() exit(self.RUN_OK) def command_enable(self, command): """ Check's if the command is enable and execute then :param command: :return: Return True if enable otherwise False """ if command.lower() == self.olt.get_write_enable().lower(): if self.olt.run_enable(): self.stdin.write(self.olt.get_write_enable() + self.NEW_LINE_UNIX) if self.olt.run_enable_password(): self.read_data(self.olt.get_expected_enable_password()) self.stdin.write(self.password_enable + self.NEW_LINE_UNIX) self.read_data(self.olt.get_expected_cardinal()) return True return False def command_quit(self, command): """ Check's if the command is quit and execute then :param command: :return: Return True if enable otherwise False """ if command.lower() == self.olt.get_write_exit().lower(): try: self.stdin.write(self.olt.get_write_exit() + self.NEW_LINE_UNIX) position = self.read_data([self.olt.get_expected_cardinal(), self.olt.get_expected_exit()]) if position == 2: # quit terminal self.stdin.write(self.olt.get_write_exit_confirmation() + self.NEW_LINE_UNIX) self.stdin.flush() self.QUIT = False # except self.tn.error, e: except IOError, e: if e.errno == errno.EPIPE: self.QUIT = False return True return False def command_execute(self, command, expected): """ Check's the command :param command: :param expected: Expected string to stop listening """ self.command_print(command) if not self.command_enable(command) and not self.command_quit(command): command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX) if command.__len__() > 0: self.stdin.write(command + self.NEW_LINE_UNIX) self.stdin.flush() self.read_data(expected) def read_data(self, character): """ Read channel waiting parameter character :param character: The character """ buffer_size = 1024 all_data = "" stop = False position = 1 if isinstance(character, types.StringType): character = [character] while not self.stdout.channel.exit_status_ready() and not stop: all_data += self.stdout.channel.recv(buffer_size) while self.stdout.channel.recv_ready(): all_data += self.stdout.channel.recv(buffer_size) nc = 1 for ch in character: if ch in all_data: stop = True position = nc break nc += 1 print(str(all_data)) self.all_data = self.all_data + str(all_data) return position def connection_old(self): """ Old method """ while 1: line = sys.stdin.readline() if not line: break self.command_execute(line, self.olt.get_expected_cardinal()) time.sleep(1) if not self.QUIT: return def connection_data(self): """ Recive data separeted by ; """ # $(sed ':a;N;$!ba;s/\n/;/g' file) content = self.data.split(";") for line in content: self.command_execute(line, self.olt.get_expected_cardinal()) if not self.QUIT: return def connection_file(self): """ Execute command from file """ with open(self.file_name) as f: content = f.readlines() for line in content: self.command_execute(line, self.olt.get_expected_cardinal()) if not self.QUIT: return