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 self.debug = False 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 print ("Logged in") 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() nc = 0 while self.QUIT: if nc == 20: self.QUIT = False # send quit to terminal until exit confirmation nc += 1 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(): if self.debug: print "Send: " print self.olt.get_write_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()) if self.debug: print "Send: " print self.password_enable 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: if self.debug: print "Send: " print self.olt.get_write_exit() 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 if self.debug: print "Send: " print self.olt.get_write_exit_confirmation() 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 "\\n" in command and len(command) == 4: print "Sendig ENTER \\r\\n" command = '\r\n' else: command = command.rstrip(self.NEW_LINE_WINDOWS).rstrip(self.NEW_LINE_UNIX) if not self.command_enable(command) and not self.command_quit(command): if command.__len__() > 0: if self.debug: print "Send: " print command 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] if self.debug: print "Expected: " print character print "Recived:" while not self.stdout.channel.exit_status_ready() and not stop: all_data += self.stdout.channel.recv(buffer_size) if self.debug: print all_data while self.stdout.channel.recv_ready(): all_data += self.stdout.channel.recv(buffer_size) if self.debug: print all_data nc = 1 for ch in character: if ch is not None and 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