看到老刘每登录一个服务器,都得在~/bin目录下搞一个命令,很是不方便。分享一下我之前从网上收藏的一个交互式登录脚本,略有改进。它有如下特性:
– 支持自定义协议版本、端口、用户名
– 如果版本是2,则用ssh2进行登录
– 支持登录newsmth的bbs
详细如下:
#! /bin/bash
declare -A HOSTS
# 格式 HOST:USER:协议版本:PORT
HOSTS[mydev-110]=”:10.0.1.110″
HOSTS[mydev-39]=”:10.0.1.39″
HOSTS[DevNet-220]=”user_01:10.6.207.220:2:36000″
HOSTS[DevNet-220strongzhou]=”strongzhou:10.6.207.220:2:36000″
HOSTS[DevNet-32]=”user_02:10.6.222.32:2:36000″
HOSTS[smzz]=”:smzz.org:”
HOSTS[newsmth]=’newsmth’PS3=”host to connect> ”
#select i in “${!HOSTS[@]}”
#do
# ssh $USER@${HOSTS[$i]}
# break;
#doneselect i in “${!HOSTS[@]}”
do
LINE=${HOSTS[$i]}
HOST=`echo “$LINE”|awk -F: ‘{print $2}’`
USER=`echo “$LINE”|awk -F: ‘{print $1}’`
VERSION=`echo “$LINE”|awk -F: ‘{print $3}’`
PORT=`echo “$LINE”|awk -F: ‘{print $4}’`CMD=”ssh”
if [ “$VERSION” = “2” ] ; then
CMD=”ssh2″
fiif [ “$PORT” != “” ] ; then
CMD=”${CMD} -p $PORT”
fiif [ “$USER” != “” ]; then
CMD=”${CMD} $USER@”
else
CMD=”$CMD ”
fiCMD=”${CMD}$HOST”
if [ “$i” = “newsmth” ] ; then
CMD=’luit -encoding gbk ssh -1 xxx@newsmth.net’
fiecho $CMD
$CMD
break;
done
可以放在你的bin目录下,比如命名为s,效果如下:
$ s 1) DevNet-220 4) DevNet-32 7) mydev-39 2) smzz 5) newsmth 3) mydev-110 6) DevNet-220strongzhou host to connect>