2017-02-20 48 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]