使用Paramiko实现跳转主机(端口转发)所涉及的主机/ IP地址和端口的说明

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

我正在尝试与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_addrjumpbox_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()

谢谢!

python ssh paramiko portforwarding
1个回答
1
投票

[jumpbox_public_addr是您的跳转服务器的地址,应该是jump.csail.mit.edu

jumpbox_private_addrsrc_addrTransport.open_channel参数)是从Transport.open_channel到目标服务器的连接的源地址。通常,您无需担心(因为您无需考虑大多数TCP连接的源地址和端口)。并且它绝对不应该是端口22。以下应告诉服务器使用默认值:

jump.csail.mit.edu
© www.soinside.com 2019 - 2024. All rights reserved.