| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- # encoding: utf-8
- import logging
- import subprocess
- from . import logger
- logger = logger.new(__name__)
- class StderrException(Exception):
- pass
- class SSHClient(object):
- def __init__(self, host, user, private_key_f, port=22):
- self.host = host
- self.port = port
- self.user = user
- self.private_key_file = private_key_f
- self.client = self.new_ssh_client()
- def get_host(self):
- return self.host
- def get_hostname(self):
- try:
- return self.exec_command('hostnamectl hostname').strip()
- except:
- return self.exec_command('hostname').strip()
- def get_user(self):
- return self.user
- def get_port(self):
- return self.port
- def get_private_key_file(self):
- return self.private_key_file
- def new_ssh_client(self):
- def cli_w(cmd):
- user_host = "%s@%s" % (self.get_user(), self.get_host())
- args = ["ssh",
- "-p", str(self.get_port()),
- "-o", "LogLevel=error",
- "-o", "StrictHostKeyChecking=no",
- "-o", "UserKnownHostsFile=/dev/null",
- "-o", "ForwardX11=no",
- "-i", self.get_private_key_file(),
- user_host,
- cmd]
- debug_args = [x for x in args]
- debug_args[-1] = "'%s'" % debug_args[-1]
- debug_cmd = ' '.join(debug_args)
- print(debug_cmd)
- ssh = subprocess.Popen(args,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = ssh.communicate()
- out = stdout.decode('utf-8')
- err = stderr.decode('utf-8')
- return out, err
- return cli_w
- # def new_ssh_client(self):
- # import paramiko
- #
- # k = paramiko.RSAKey.from_private_key_file(self.private_key_file)
- # c = paramiko.SSHClient()
- # c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- # c.connect(
- # hostname=self.host,
- # port=self.port,
- # username=self.user,
- # pkey=k,
- # banner_timeout=60,
- # timeout=60)
- # return c
- def exec_command(self, command, use_sudo=False):
- command = "bash -c '%s %s'" % ('[ -s /etc/kubernetes/admin.conf ] && export KUBECONFIG=/etc/kubernetes/admin.conf || :;', command)
- if use_sudo:
- command = f'sudo {command}'
- logger.info("exec_command: %s" % command)
- # _, stdout, stderr = self.client.exec_command(command)
- out, err = self.client(command)
- if err:
- raise StderrException(err)
- return out
|