SSH 到服务器并从那里 SCP 到另一台服务器

问题描述 投票:0回答:1

我想通过 SSH 连接到 Linux 服务器,然后从那里将 scp 复制到一台 ESXI 服务器。两者都需要用户名和密码。

  1. 连接到linux盒子:

    ssh_linux = paramiko.SSHClient()
    ssh_linux.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_linux.connect(linux_host, username=linux_user, password=linux_password)
    
  2. SCP:

    scp_command = f"scp -o StrictHostKeyChecking=no {local_file_path} {esxi_user}@{esxi_host}:{remote_file_path}"
    logging.info(f"{scp_command}")
    stdin, stdout, stderr = ssh_linux.exec_command(scp_command)
    scp_exit_status = stdout.channel.recv_exit_status()
    
    if scp_exit_status == 0:
        logging.info(f"File {file} copied successfully.")
    else:
        logging.error(f"Error copying. SCP command failed with exit code {scp_exit_status}")
        logging.error(f"SCP STDOUT: {stdout.read().decode()}")
        logging.error(f"SCP STDERR: {stderr.read().decode()}")
        ssh_linux.close()
    

如何将ESXI主机的密码发送到scp命令?

python paramiko
1个回答
0
投票

请参阅 使用 Python Paramiko 通过 SSH 将输入/变量传递到命令/脚本

尽管 OpenSSH 工具不接受来自标准输入的密码。您必须通过将

get_pty=True
传递给
SSHClient.exec_command
来伪造终端。


总的来说,我认为这不是正确的方法。

相反,我会使用端口转发。请参阅使用 Python Paramiko 的嵌套 SSH

© www.soinside.com 2019 - 2024. All rights reserved.