使用 nc 和 no_proxy 变量通过代理进行 SSH

使用 nc 和 no_proxy 变量通过代理进行 SSH

我使用 HTTP 代理,并尝试通过 ssh 从我的 Ubuntu 笔记本连接到远程计算机。如果我将 .ssh/config 配置为

Host test
HostName  55.66.77.88
ProxyCommand nc -X connect -x proxy.corp.com:8080 %h %p
User test_user

我可以连接到远程机器。我的网络上的其他用户(使用 OSX)可以连接到同一台机器,只需将机器的 IP 添加到变量中no_proxy,而无需在 ssh/config 上使用 nc。如果我将机器添加到我的no_proxy变量上zshenv

no_proxy="localhost,127.0.0.1,55.66.77.88"
NO_PROXY=$no_proxy

并检查我的终端,变量已设置(导出后):

no_proxy=localhost,127.0.0.1,55.66.77.88

但现在我无法连接到机器。可能出了什么问题?我相信变量no_proxy被忽略了,但我无法测试它。

编辑在配置文件中添加没有 nc 行的 ssh 会话:

ssh 55.66.77.88 -v
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/user/.ssh/config
debug1: /home/user/.ssh/config line 1: Applying options for *
debug1: /home/user/.ssh/config line 16: Applying options for 55.66.77.88
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 55.66.77.88 [55.66.77.88] port 22.

并在文件ncconfig

OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/user/.ssh/config
debug1: /home/user/.ssh/config line 1: Applying options for *
debug1: /home/user/.ssh/config line 16: Applying options for 55.66.77.88
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Executing proxy command: exec nc -X connect -x proxy.corporation.br:8080 55.66.77.88 22
debug1: permanently_drop_suid: 1000
debug1: identity file /home/user/.ssh/id_rsa type 1
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/user/.ssh/id_ed25519 type -1
debug1: identity file /home/user/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4+deb7u2
debug1: match: OpenSSH_6.0p1 Debian-4+deb7u2 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Host '[55.66.77.88]:22' is known and matches the ECDSA host key.
debug1: Found key in /home/user/.ssh/known_hosts:7
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/user/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/user/.ssh/id_dsa
debug1: Trying private key: /home/user/.ssh/id_ecdsa
debug1: Trying private key: /home/user/.ssh/id_ed25519
debug1: Next authentication method: password
[email protected]'s password: 
debug1: Authentication succeeded (password).
Authenticated to 55.66.77.88 (via proxy).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LC_PAPER = pt_BR.UTF-8
debug1: Sending env LC_MONETARY = pt_BR.UTF-8
debug1: Sending env LC_TELEPHONE = pt_BR.UTF-8
debug1: Sending env LC_NAME = pt_BR.UTF-8
debug1: Sending env LANG = pt_BR.UTF-8
debug1: Sending env LC_NUMERIC = pt_BR.UTF-8
debug1: Sending env LC_MEASUREMENT = pt_BR.UTF-8
debug1: Sending env LC_TIME = pt_BR.UTF-8
debug1: Sending env LC_IDENTIFICATION = pt_BR.UTF-8
debug1: Sending env LC_ADDRESS = pt_BR.UTF-

谢谢

答案1

我认为你误解了环境变量的用法NO_PROXY。这意味着你应该不是连接到变量中列出的主机/域/IP 时使用 HTTP 代理服务器。对于位于 的远程主机55.66.77.88,我相信您需要代理,至少在限制网络内时。

当您运行实验时,无法与 中的 IP 建立连接,您的文件中NO_PROXY是否仍有该行?如果是,请删除 IP 并重试。 ProxyCommand.ssh/config

或者,如果您试图避免更改每个需要代理的主机的 ssh 配置文件,则可以向将Host *通过自定义脚本进行代理的条目添加指令。该脚本只能通过代理连接到它无法直接访问的主机。我的 github 上有一个脚本(称为 ssh-proxy),它可以完成这些任务,甚至更多。

相关内容