如何通过SOCKS代理运行fabric脚本?

1 投票
4 回答
2560 浏览
提问于 2025-04-16 15:12

我有一个SOCKS代理,它连接到一个网关服务器。这个代理是通过在我的ssh_config文件中设置一个主机定义来创建的,使用了DynamicForward localhost:9876。为了通过SSH连接到远程服务器,我又设置了另一个主机定义,使用ProxyCommand /usr/bin/nc -x localhost:9876 %h %p,这一切都运行得很好。

不过,我的fabric脚本需要连接到那个远程服务器。我该如何告诉它在连接时使用SOCKS代理呢?

4 个回答

0

你可以使用tsocks和OpenSSH客户端自带的功能来创建SOCKS隧道。这需要一些配置,但效果很好。下面是我在Ubuntu 10.04上开始的步骤。

# Install the tsocks library and shell script.
sudo apt-get install tsocks

# Configure the range of IP addresses you need access to.
sudo nano /etc/tsocks.conf

# Use the OpenSSH client to create a socks proxy (stepping
# stones are hosts used to gain access to private subnets).
ssh -D 1080 stepping.stone

# Now connect to any given address in your configured range.
# tsocks will intercept the connection and route it using
# the SOCKS proxy we created with the previous command.
tsocks ssh 1.2.3.4

Fabric在没有VPN连接的情况下无法工作,这让我很头疼,所以这个解决方案非常不错;只需要对一个主机有SSH访问权限就可以了。

2

Fabric的SSH层目前不支持网关或代理,但他们“可能会在某个时候修补/ 分支它,以添加这个功能。”(来自这里)。

Fabric还有一个未解决的问题,想要实现隧道功能

这篇博客建议重写运行函数。

2

隧道功能现在(2013年3月)已经被Fabric原生支持了:可以查看这个讨论,它引导了代码的变化,还有这个提交信息,里面有一些背景说明。

撰写回答