脚本 sshmgr.sh

#!/bin/bash

conf=/Users/h/Tool/sshmgr.conf
set sshname=
set sshhost=
menu() {
while read line
do
if [ "${line:0:1}" == "#" ];then
continue
fi

if [ "${line}z" == "z" ];then
continue
fi

if [ "${line:0:2}" == "--" ];then
echo ''
continue
fi
# # 序号 | 显示名称 | IP | 端口 | 用户名 | 密码 | key
id=`echo $line|awk -F "|" '{print $1}'`
name=`echo $line|awk -F "|" '{print $2}'`
ip=`echo $line|awk -F "|" '{print $3}'`
echo $id $name $ip
done < ${conf}
}

link() {
while read line
do
if [ "${line:0:1}" == "#" ];then
continue
fi

if [ "${line}z" == "z" ];then
continue
fi

if [ "${line:0:2}" == "--" ];then
continue
fi

# # 序号 | 显示名称 | IP | 端口 | 用户名 | key
id=`echo $line|awk -F "|" '{print $1}'`
name=`echo $line|awk -F "|" '{print $2}'`
host=`echo $line|awk -F "|" '{print $3}'`
port=`echo $line|awk -F "|" '{print $4}'`
username=`echo $line|awk -F "|" '{print $5}'`
key=`echo $line|awk -F "|" '{print $6}'`

if [ "$id" -eq "$1" ];then
sshname=$name
sshhost=`echo $username@$host -p $port -i $key`
break
fi
done < ${conf}
}

while true; do
menu
read -p '输入进入的服务器:' cmd

if [ "${cmd}" == "0" ];then
break
fi

sshhost=
link $cmd

if [ -z "${sshhost}" ];then
echo '选择的服务器不存在,请重新选择.'
else
echo 正在链接[$name]服务器...
ssh $sshhost
fi
done

配置文件 sshmgr.conf

# 序号 | 显示名称 | IP | 端口 | 用户名 | key 
1|namea|192.168.1.1|22|root|~/.ssh/id_rsa

快捷启动.zshrc

alias ssmgr='/Users/h/Tool/sshmgr.sh'

参考