我需要通过jumphost ssh到远程机器。
使用ssh我是这样做的: ssh -A -t user@jumphost ssh -A user@vm_to_login
如果我只想运行一些命令,我会运行: ssh -A -t user@jumphost ssh -A user@vm_to_login“command to execute”
现在,我尝试使用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 montana@vm_to_run_command -A "docker network inspect --format '{{json .Containers }}' bridge" """, get_pty=True)当我运行上面的脚本时,程序挂起了无限的时间。我只是猜测SSHClient对象不能将密钥添加到ssh代理,当第二个服务器查找密钥时,密钥请求转到jumpbox,并从jumpbox将其一些添加到我的本地,但SSHClient对象确实有这些密钥。
如果需要更多的信息,请让我知道。
发布于 2017-09-18 22:29:32
我已经得到了如何使用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-将路径添加到私钥。ex - ~/.ssh/id_rsa
https://stackoverflow.com/questions/42348704
复制相似问题