ssh_client.py 1005 B
import os
from pexpect import pxssh
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh
class Cmd:
def __init__(self, cmd: str, do_print: bool = True, prefix: str = ""):
self.cmd = cmd
self.do_print = do_print
self.prefix = prefix
class SSHClient:
def __init__(self):
self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json")
def run_cmd(self, client: int, cmd: list):
try:
s = pxssh.pxssh()
dest = self.clients[client]
s.login(dest.hostname, dest.user, dest.password)
for command in cmd:
s.sendline(command.cmd)
s.prompt()
if command.do_print:
print(command.prefix, s.before.decode('utf-8'))
s.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)