Beispiel #1
0
def runSshCmd(cmd, host, user="", mpprcFile="", timeout=""):
    """
    function: run ssh cmd
    input  : cmd, host, user, mpprcFile, timeout
    output : str
    """
    if (timeout):
        timeout = "-o ConnectTimeout=%s" % timeout
    if (mpprcFile):
        cmd = "source '%s'; %s" % (mpprcFile, cmd)
    # Set the output LANG to English
    cmd = "export LC_ALL=C; %s" % cmd
    # RedHat does not automatically source /etc/profile
    # but SuSE executes when using ssh to remotely execute commands
    # Some environment variables are written in /etc/profile
    # when there is no separation of environment variables
    if (host == DefaultValue.GetHostIpOrName()):
        sshCmd = cmd
    else:
        sshCmd = "pssh -s -H %s %s 'source /etc/profile 2>/dev/null;%s'" % (
            host, timeout, cmd)
    if (user and user != getCurrentUser()):
        sshCmd = "su - %s -c \"%s\"" % (user, sshCmd)
    (status, output) = subprocess.getstatusoutput(sshCmd)
    if (status != 0):
        raise SshCommandException(host, sshCmd, output)
    return output
Beispiel #2
0
def runRootCmd(cmd, rootuser, passwd, mpprcFile=''):
    """
    function: run root cmd
    input  : cmd, rootuser, passwd, mpprcFile
    output : str
    """
    if (mpprcFile):
        cmd = "source '%s'; %s" % (mpprcFile, cmd)
    ssh = None
    try:
        import paramiko
        cmd = "export LC_ALL=C; source /etc/profile 2>/dev/null; %s" % cmd
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect('localhost', 22, rootuser, passwd)
        stdout, stderr = ssh.exec_command(cmd, get_pty=True)[1:3]
        output = stdout.read()
        error = stderr.read()
        if error:
            raise SshCommandException(cmd, "localhost", error)
        return output
    except Exception as e:
        raise Exception(str(e))
    finally:
        if ssh:
            ssh.close()
Beispiel #3
0
def runSshCmdWithPwd(cmd, host, user="", passwd="", mpprcFile=""):
    """
    function: run ssh cmd with password
    input  : cmd, host, user, passwd, mpprcFile
    output : str
    """
    # Environment variables separation
    if (mpprcFile):
        cmd = "source '%s'; %s" % (mpprcFile, cmd)
    ssh = None
    try:
        if (passwd):
            import paramiko
            cmd = "export LC_ALL=C; source /etc/profile 2>/dev/null; %s" % cmd
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            # Remote Connection
            ssh.connect(host, 22, user, passwd)
            stdout, stderr = ssh.exec_command(cmd)[1:3]
            output = stdout.read()
            error = stderr.read()
            if error:
                raise SshCommandException(host, cmd, error)
            return output.decode()
        else:
            cmd = \
                "pssh -s -H %s \"export LC_ALL=C; " \
                "source /etc/profile 2>/dev/null; %s\"" % (
            host, cmd)
            (status, output) = subprocess.getstatusoutput(cmd)
            if (status != 0):
                raise SshCommandException(host, cmd, output)
            return output
    except Exception as e:
        raise Exception(str(e))
    finally:
        if (ssh):
            ssh.close()