背景
一般情况下,我们ssh 配置都是使用 ~/.ssh/config
里面的配置即可。
但是如果我们线上所要维护或者要连接的机器比较多,我们怎么分门别类呢?
大家请看:
1
2
|
Include
Include the specified configuration file(s). Multiple path‐names may be specified and each pathname may contain glob(3) wildcards and, for user configurations, shell-like ‘~’ references to user home directories. Files without absolute paths are assumed to be in ~/.ssh if included in a user configuration file or /etc/ssh if included from the system configuration file. Include directive may appear inside a Match or Host block to perform conditional inclusion.
|
注意: Include
必须在 config
配置的 Host
之前的位置,不能夹杂在多个 Host
之间。
涉及到的脚本:
Makefile
1
2
|
install-ssh-config:
@./install-ssh-config.sh $(identify_file)
|
install-ssh-config.sh:
1
2
3
4
5
6
7
|
identify_file=${1:-""}
if [ ! -z $identify_file ]; then
identify_file="IdentityFile $identify_file"
fi
...
grep -q -F 'Include ~/.ssh/config.d/*' "$HOME/.ssh/config" || cat -n "$HOME/.ssh/config" |grep -i host |awk '{print $1-1}'|sed -n "1"p |xargs -I LINE sed -i '' -e $'LINE i\\\n''Include ~/.ssh/config.d/*' "$HOME/.ssh/config"
|
分步分析:
- Makefile 的参数传递,可以通过带指定参数名来传递给 shell;
- shell 通过
$参数位置
读取,更多详解请查阅文档;
- grep -q 的命令;
- grep -F ;
- grep -i 不区分大小写;
- awk 获取参数值;
- sed -n “1"p 获取到第 1 个值并打印出来;
- xargs -I LINE 将结果通过管道带入到下一个语句中,并赋值给 LINE;
sed -i '' -e $'LINE i\\\n''xxx'
在匹配的位置插入 “xxx”
参考资料
- What's wrong with my OpenSSH Include directive?
- ssh_config — OpenSSH SSH client configuration files
茶歇驿站
一个可以让你停下来看一看,在茶歇之余给你帮助的小站,这里的内容主要是后端技术,个人管理,团队管理,以及其他个人杂想。