ssh_util.py 3.33 KB
Newer Older
chenyuanjie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import os
import re
import sys

sys.path.append(os.path.dirname(sys.path[0]))

import paramiko
from paramiko import SSHClient


class SSHUtil(object):
    """
    ssh工具类
    """

    __sqoop_home__ = "hadoop5"
    __python_home__ = "hadoop5"

    __ssh_h5_host__ = "hadoop5"
    __ssh_h5_port__ = 22
    __ssh_h5_user__ = "root"
chenyuanjie committed
22
    __ssh_h5_pwd__ = "3$Xf9*kLp2Q"
chenyuanjie committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    @staticmethod
    def get_ssh_client():
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname=SSHUtil.__ssh_h5_host__, port=SSHUtil.__ssh_h5_port__, username=SSHUtil.__ssh_h5_user__,
                       password=SSHUtil.__ssh_h5_pwd__)
        return client

    @staticmethod
    def exec_command_async(client: SSHClient, cmd: str, ignore_err: bool = False):
        """
        :param client:
        :param cmd:
        :return: 成功返回shout 失败返回 sherr
        """
        print("============================执行脚本中=================================")
        print(cmd)
        channel = client.invoke_shell()
        channel.keep_this = client
        stdin = channel.makefile('wb')
        stdout = channel.makefile('r')

        cmd = cmd.strip('\n')
        stdin.write(cmd + '\n')
        finish = 'end of stdOUT buffer. finished with exit status'
        special_symbol = '\x1b'
        echo_cmd = 'echo {} $?'.format(finish)
        stdin.write(echo_cmd + '\n')
        # shin = self.stdin
        stdin.flush()

        shout = []
        sherr = []
        exit_status = 0
        for line in stdout:
            print(line.replace("\n", ""))
            if str(line).startswith(cmd) or str(line).startswith(echo_cmd):
                # up for now filled with shell junk from stdin
                shout = []
            elif str(line).startswith(special_symbol):
                continue
            elif str(line).startswith(finish):
                # our finish command ends with the exit status
                exit_status = int(str(line).rsplit(maxsplit=1)[1])
                if exit_status:
                    # stderr is combined with stdout.
                    # thus, swap sherr with shout in a case of failure.
                    sherr = shout
                    shout = []
                break
            else:
                # get rid of 'coloring and formatting' special characters
                shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).
                             replace('\b', '').replace('\r', ''))

        # first and last lines of shout/sherr contain a prompt
        if shout and echo_cmd in shout[-1]:
            shout.pop()
        if shout and cmd in shout[0]:
            shout.pop(0)
        if sherr and echo_cmd in sherr[-1]:
            sherr.pop()
        if sherr and cmd in sherr[0]:
            sherr.pop(0)

        err_msg = ''.join(sherr)
        success_msg = ''.join(shout)
        success_flag = success_msg is not None and success_msg != ''
        msg = success_msg or err_msg
        if ignore_err == False and success_flag == False:
            print(msg)
            raise Exception("脚本执行出错!出错原因详情如上!")
        channel.close()
        return {
            "success": success_flag,
            "msg": msg,
            "exit_status": exit_status
        }


if __name__ == '__main__':
    pass