def ssh_shell_with_password_input_paramiko()

in deployment/k8sPaiLibrary/maintainlib/common.py [0:0]


def ssh_shell_with_password_input_paramiko(host_config, commandline):

    hostip = str(host_config['hostip'])
    if ipv4_address_validation(hostip) == False:
        return False
    if 'password' not in host_config and 'keyfile-path' not in host_config:
        logger.error("At least, you should config a password or ssh key file path for a node.")
        logger.error("Both password and ssh key file path are missing in the node [ {0} ].".format(host_config['hostip']))
        return False

    username = str(host_config['username'])
    password = None
    if 'password' in host_config:
        password = str(host_config['password'])
    port = 22
    if 'sshport' in host_config:
        if port_validation(host_config['sshport']) == False:
            return False
        port = int(host_config['sshport'])
    key_filename = None
    if 'keyfile-path' in host_config:
        if os.path.isfile(str(host_config['keyfile-path'])) and host_config['keyfile-path'] is not None:
            key_filename = str(host_config['keyfile-path'])
        else:
            logger.warn("The key file: {0} specified doesn't exist".format(host_config['keyfile-path']))

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostip, port=port, key_filename=key_filename, username=username, password=password)
    password = password if password is not None else ''
    if (commandline.strip().startswith('sudo')):
        commandline = commandline.replace('sudo', 'sudo -S', 1)
    stdin, stdout, stderr = ssh.exec_command("echo '{0}' | {1}".format(password, commandline), get_pty=True)
    logger.info("Executing the command on host [{0}]: {1}".format(hostip, commandline))
    for response_msg in stdout:
        print (response_msg.encode('utf-8').strip('\n'))

    exit_code_ssh = stdout.channel.recv_exit_status()
    if exit_code_ssh != 0:
        sys.exit(exit_code_ssh)

    ssh.close()
    return True