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

2024-03-28 14:40:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢大家!


Tags: keyparamikotargetmitprivatepublicfilenamessh
1条回答
网友
1楼 · 发布于 2024-03-28 14:40:08

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

jumpbox_private_addr^{}src_addr参数是从jump.csail.mit.edu到目标服务器的连接的源地址。一般来说,您并不关心这一点(因为您不关心大多数TCP连接的源地址和端口)。而且绝对不应该是22号端口。以下内容应告诉服务器使用默认值:

src_addr = ("0.0.0.0", 0)

相关问题 更多 >