# # ---------------------------------------------------------- """ """ import os import ulrich import paramiko def getRemoteClass(comp): job = ulrich.program.Job.getInstance() verify = job.getDebugLevel("config_tool") if job.conf.confs.get("tools").get("remotetyp") == "ssh": return SshCmd(comp) class RemoteCmd: def __init__(self): self.rc = 0 self.sysout = "" pass class SshCmd(RemoteCmd): def __init__(self, comp): self.conn = comp def execCmd(self, cmds): """ :param cmds: :return: """ ssh = paramiko.SSHClient() ssh.load_system_host_keys(os.path.expanduser('~/.ssh/known_hosts')) if self.conn["password"]: ssh.connect(self.conn["host"], username=self.conn["user"], password=self.conn["password"]) else: ssh.connect(self.conn["host"], username=self.conn["user"]) shell = ssh.invoke_shell() for cmd in cmds: stdin, stdout, stderr = ssh.exec_command(cmd + "\n") self.sysout = stdout.read() stdin.close() stderr.close() stdout.close() ssh.close()