我正在尝试与Paramiko设置跳转主机连接。
这是我在~/.ssh/config
中的设置
Host jump.csail.mit.edu
GSSAPIAuthentication yes
GSSAPIKeyExchange yes
VerifyHostKeyDNS yes
Host *.csail.mit.edu !jump.csail.mit.edu 128.52.* 128.30.* 128.31.*
ProxyCommand ssh -W %h:%p jump.csail.mit.edu
GSSAPIAuthentication yes
GSSAPIDelegateCredentials yes
GSSAPIKeyExchange yes
并且如果我从终端连接也可以。
我还为Paramiko跳转主机连接找到了this code,但我想知道基于上述ssh设置应如何设置jumpbox_public_addr
和jumpbox_private_addr
?
import os
import paramiko
ssh_key_filename = os.getenv('HOME') + '/.ssh/id_rsa'
jumpbox_public_addr = '168.128.52.199'
jumpbox_private_addr = '10.0.5.10'
target_addr = '10.0.5.20'
jumpbox=paramiko.SSHClient()
jumpbox.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jumpbox.connect(jumpbox_public_addr, username='root', key_filename=ssh_key_filename)
jumpbox_transport = jumpbox.get_transport()
src_addr = (jumpbox_private_addr, 22)
dest_addr = (target_addr, 22)
jumpbox_channel = jumpbox_transport.open_channel("direct-tcpip", dest_addr, src_addr)
target=paramiko.SSHClient()
target.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target.connect(target_addr, username='root', key_filename=ssh_key_filename, sock=jumpbox_channel)
stdin, stdout, stderr = target.exec_command("ifconfig")
for line in stdout.read().split(b'\n'):
print(str(line))
target.close()
jumpbox.close()
谢谢!
[jumpbox_public_addr
是您的跳转服务器的地址,应该是jump.csail.mit.edu
。
jumpbox_private_addr
(src_addr
的Transport.open_channel
参数)是从Transport.open_channel
到目标服务器的连接的源地址。通常,您无需担心(因为您无需考虑大多数TCP连接的源地址和端口)。并且它绝对不应该是端口22。以下应告诉服务器使用默认值:
jump.csail.mit.edu