1---2title: "ssh_config 整理"3date: 2023-01-28T14:30:02+08:004---5678自定义函数,将下面的脚本加入到 `bashrc`/`zshrc` 之类的配置中去。9> 脚本根据我的使用习惯编写,并不完全通用,如有需要,自行修改。10> 值得注意的地方有:11> + Host/HostName/Port 大小写敏感12> + 使用空格缩进。1314```15sshhosts () {16 IFS=$'\n'17 break=''18 for line in $(grep --ignore-case -E "^ *Host|^ *HostName |^ *Port" ~/.ssh/config)19 do20 line=$(sed 's/^[[:space:]]*//' <<< "$line")21 value=$(echo $line | cut -d " " -f2- | tr -d $'\n')22 [[ $line =~ Host[[:space:]].* ]] && printf "%s%-10s" "$break" "$value"23 [[ $line =~ HostName[[:space:]].* ]] && printf " %s" $value24 [[ $line =~ Port[[:space:]].* ]] && printf ":%s" $value25 break=$'\n'26 done27 echo ""28}29```3031使用示例:32```33> sshhosts34ringo 10.233.2.335seiran 10.233.2.436reisen 10.233.2.637tei 10.233.2.2:2323338dev 10.233.2.6:202239```4041如果你有 `Host github.com` 一类,不是用于登陆的配置,不希望它们污染 host 列表,可以通过 include 指令,将配置分割成多个文件。4243```44> head -n 5 config45include ~/.ssh/config.d/*.conf4647Host ringo48 HostName 10.233.2.349 User lindsay50>51>52> tree53.54├── authorized_keys55├── config56└── config.d57 ├── application.conf58 ├── deprecated.conf59 ├── general.conf60 └── git.conf61622 directories, 6 files63>64>65> cat config.d/general.conf66Host *67 ServerAliveCountMax 2068 ServerAliveInterval 3069 HostKeyAlgorithms +ssh-rsa70 PubkeyAcceptedAlgorithms +ssh-rsa71 IdentityFile ~/.ssh/mykey.pem72```