配置
端口转发步骤(通过命令行)
Paramiko研究
我看过与使用Paramiko进行端口转发相关的一把 of 问题,但它们似乎没有解决这种特殊情况。
我的问题
如何使用Paramiko运行上面的步骤2和3?我基本上想跑:
import paramiko
# Create the tunnel connection
tunnel_cli = paramiko.SSHClient()
tunnel_cli.connect(PROXY_HOSTNAME, PROXY_PORT, PROXY_USER)
# Create the forwarded connection and issue commands from LOCAL on the REMOTE box
fwd_cli = paramiko.SSHClient()
fwd_cli.connect('localhost', LOCAL_PORT, REMOTE_USER)
fwd_cli.exec_command('pwd')发布于 2013-09-26 22:27:51
关于Paramiko正在做什么的详细解释可以在@bitprohet的博客找到。
假设上面的配置,我所使用的代码如下所示:
from paramiko import SSHClient
# Set up the proxy (forwarding server) credentials
proxy_hostname = 'your.proxy.hostname'
proxy_username = 'proxy-username'
proxy_port = 22
# Instantiate a client and connect to the proxy server
proxy_client = SSHClient()
proxy_client.load_host_keys('~/.ssh/known_hosts/')
proxy_client.connect(
proxy_hostname,
port=proxy_port,
username=proxy_username,
key_filename='/path/to/your/private/key/'
)
# Get the client's transport and open a `direct-tcpip` channel passing
# the destination hostname:port and the local hostname:port
transport = proxy_client.get_transport()
dest_addr = ('0.0.0.0', 8000)
local_addr = ('127.0.0.1', 1234)
channel = transport.open_channel("direct-tcpip", dest_addr, local_addr)
# Create a NEW client and pass this channel to it as the `sock` (along with
# whatever credentials you need to auth into your REMOTE box
remote_client = SSHClient()
remote_client.load_host_keys(hosts_file)
remote_client.connect('localhost', port=1234, username='remote_username', sock=channel)
# `remote_client` should now be able to issue commands to the REMOTE box
remote_client.exec_command('pwd')发布于 2013-09-24 00:37:52
是否只需要从代理中弹出SSH命令,还是也需要转发其他非SSH端口?
如果您只需要将SSH放入远程框中,Paramiko就支持SSH级别的网关(告诉代理sshd代表本地打开到远程和转发SSH通信量的连接)和ProxyCommand支持(通过本地命令转发所有SSH通信量,这可以是任何能够与远程框对话的东西)。
听起来你想要前者,因为代理显然已经运行了sshd。如果您查看一份Fabric副本并搜索“gateway”,您会发现Fabric是如何使用Paramiko的网关支持的(我现在没有时间自己挖出具体的点)。
https://stackoverflow.com/questions/18968069
复制相似问题