简体   繁体   English

带有ProxyCommand和BASH脚本的SSH配置动态端口

[英]SSH Config Dynamic Port with ProxyCommand and BASH Script

I've got a server setup to randomise it's SSH Port after a certain amount of time and publish the port to a .txt file available on it's web server. 我有一个服务器设置,可以在一定时间后将它的SSH端口随机化,并将端口发布到Web服务器上可用的.txt文件中。 I have then written a simple script on my client which fetches the new port from the web server .txt file and updates a specific hosts Port number in ~/.ssh/config . 然后,我在客户端上编写了一个简单的脚本,该脚本从Web服务器.txt文件获取新端口并更新〜/ .ssh / config中的特定主机端口号。

Because ~/.ssh/config cannot parse Bash variables I call the script with ProxyCommand (I am using a JumpHost and the JH Port is the dynamic Port). 因为〜/ .ssh / config无法解析Bash变量,所以我使用ProxyCommand调用了脚本(我使用的是JumpHost,而JH端口是动态端口)。

My ~/.ssh/config as follows: 我的〜/ .ssh / config如下:

Host jumphost
HostName jumphost.example.com
Port 51638
User bob
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null

Host myserver
HostName myserver.com
Port 2222
User bob
ProxyCommand ~/.ssh/get_dynamic_port.sh ssh -W %h:%p jumphost

Bash script as follows (get_dynamic_port.sh): Bash脚本如下(get_dynamic_port.sh):

#!/bin/sh
PORT=$(curl -s http://jumphost.example.com/port.txt)
OLDIP=`grep -w "jumphost.example.com" -A 1 ~/.ssh/config | awk '/Port/ {print $2}'`
LINE_NUMBER=`grep -n "jumphost.example.com" -A 1 ~/.ssh/config | grep -v "jumphost.example.com" | awk '{print $1}' FS="-"`
sed -i'.bak' -e "${LINE_NUMBER}s/$OLDIP/$PORT/" ~/.ssh/config

The script works fine and updates the Port for jumphost.example.com but unfortunately I cannot connect, ssh running in debug output below: 该脚本可以正常工作,并为jumphost.example.com更新端口,但是很遗憾,我无法连接,SSH在以下调试输出中运行:

macosx:$ ssh -vvv myserver
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data ~/.ssh/config
debug1: ~/.ssh/config line 54: Applying options for myserver
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Executing proxy command: exec ~/.ssh/get_dynamic_port.sh ssh -W myserver:2222 jumphost
debug1: identity file ~/.ssh/id_rsa type -1
debug1: identity file ~/.ssh/id_rsa-cert type -1
debug1: identity file ~/.ssh/id_dsa type -1
debug1: identity file ~/.ssh/id_dsa-cert type -1
debug1: identity file ~/.ssh/id_ecdsa type -1
debug1: identity file ~/.ssh/id_ecdsa-cert type -1
debug1: identity file ~/.ssh/id_ed25519 type -1
debug1: identity file ~/.ssh/id_ed25519-cert type -1
debug1: identity file ~/.ssh/id_xmss type -1
debug1: identity file ~/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
ssh_exchange_identification: Connection closed by remote host

It should be noted that tailing the secure log file on the jumphost server doesn't show any connection attempts which might be a sign as to what is wrong. 应该注意的是,将安全日志文件拖到Jumphost服务器上不会显示任何连接尝试,这可能是错误提示。

The jumphost config works fine without the dynamic port script, and as stated above the script is actually changing the port of the jumphost correctly but then ssh just fails after that. 在没有动态端口脚本的情况下,jumphost配置可以正常工作,并且如上所述,该脚本实际上在正确更改Jumphost的端口,但之后ssh只会失败。

Any ideas on how to achieve this or what I could be doing wrong would be appreciated. 关于如何实现这一目标或我可能做错了任何想法,将不胜感激。 I could just use a crontab entry to run the script every so often to update the jumphost port, but I would prefer to only have the jumphost port updated when a connection is being made to it, just seems a bit cleaner that way. 我可以只使用crontab条目每隔一段时间运行一次脚本来更新Jumphost端口,但是我宁愿只在建立连接时才更新Jumphost端口,这样看起来有点干净。

Thanks :) 谢谢 :)

The modern way to use "jumphosts" is with the -J option ( ProxyJump ). 使用“ jumphosts”的现代方法是使用-J选项( ProxyJump )。

Using the ProxyCommand option still works and has the flexibility to run arbitrary setup code by calling a script, as here. 仍然可以使用ProxyCommand选项,并且可以通过调用脚本灵活地运行任意安装代码,如下所示。 However, your code must, ultimately, run an appropriate ssh command to perform the "jump". 但是,您的代码最终必须运行适当的ssh命令来执行“跳转”。

The typical config option looks like: 典型的config选项如下所示:

Host jump
    Hostname jumphost.fqdn
    User juser

Host final
    Hostname final.fqdn
    User fuser
    ProxyCommand ssh -W %h:%p jump

You run ssh final which opens a connection from localhost to jump , then another from jump to final with the necessary forwarding enabled. 您运行ssh final ,它将打开从localhost到jump的连接,然后打开另一个从jumpfinal的连接,并启用了必要的转发。

In your configuration, you have replaced ProxyCommand with a shell script that performs some setup. 在您的配置中,您已用执行某些设置的Shell脚本替换了ProxyCommand You still need to run something like the normal ssh command afterwards. 之后,您仍然需要运行类似普通ssh命令的命令。

Given a config line like your: 给定像您这样的配置行:

ProxyCommand ~/.ssh/get_dynamic_port.sh ssh -W %h:%p jumphost

the simplest way to invoke the normal ssh command (which you are passing as arguments to the shell script), is to invoke it at the end: 调用普通ssh命令(作为参数传递给shell脚本)的最简单方法是在末尾调用它:

#!/bin/sh

# ... custom stuff ...

# new final line:
eval "$@"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM