2017-02-20 142 views
0

我需要通过jumphost ssh到远程机器。使用ssh使用paramiko如何将ssh密钥添加到ssh-agent多跳中

我这样做: SSH -A -t用户@ jumphost SSH -A用户@ vm_to_login

,如果我只是想运行一些命令,我​​运行: SSH -A -t用户@ jumphost SSH -A用户@ vm_to_login“命令来执行”

现在我试着这样做使用python:

def ssh_connect(jumphost_ip): 
    ssh_client=paramiko.SSHClient() 
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    path_to_key=os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') 
    ssh_client.connect(jumphost_ip, username='vagrant', key_filename=path_to_key, allow_agent=True) 
    return ssh_client 

ssh_client = ssh_connect(host_ip) 

stdin, stdout, stderr = ssh_client.exec_command("""ssh [email protected]_to_run_command -A "docker network inspect --format '{{json .Containers }}' bridge" """, get_pty=True) 

当我运行上面的脚本,程序挂无限的时间..我只是猜测SSHClient对象无法将密钥添加到ssh代理,当第二台服务器查找密钥时,密钥请求会转到跳转框,并从jumpbox跳到我的本地,但SSHClient对象确实有这些密钥。

如果需要更多信息,请让我知道。

回答

0

我有答案使用上述程序的用户使用之前的paramiko

def ssh_connect(jumphost_ip): 
    ssh_client=paramiko.SSHClient() 
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    path_to_key=os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') 
    ssh_client.connect(jumphost_ip, username='vagrant', key_filename=path_to_key, allow_agent=True) 
    s = ssh_client.get_transport().open_session() 
    # set up the agent request handler to handle agent requests from the server 
    paramiko.agent.AgentRequestHandler(s) 
    return ssh_client 

如何处理代理转发必须确保正确的私钥被添加到ssh-agent。在主机上运行命令以将私钥添加到ssh-agent: 1. eval $(ssh-agent) 2. ssh-add [私钥的路径。 ex - 〜/ .ssh/id_rsa]