Shell编写的SSH登陆管理器
时间:2008-04-07 来源:sohu2000000
#!/bin/bash
#
# Author:
# CopyRight :
#
############### Begin #################
# User Function and Variables definition
LOCALFILE="${HOME}/ssh-management"
if [ ! -f $LOCALFILE ]
then
touch $LOCALFILE
fi
[ -f /usr/bin/dialog ] || { echo "Not found dialog";exit 1; }
[ -f $LOCALFILE ] || touch $LOCALFILE
# User Program Main
function ADDSSH_HOST {
clear
echo -n "Pleast Input SSH Host IP: "
read -r ADDSSH[0]
DIGIT=(${ADDSSH[0]//[!0-9]/ })
[ "${#DIGIT[@]}" != "4" ] && {
echo "IP ADDRESS ERROR "
exit 1
}
echo -n "Pleast Input SSH Host Port (Default 22): "
read -r ADDSSH[1]
[ -z ${ADDSSH[1]} ] && ADDSSH[1]=22
if (( 0>${ADDSSH[1]} && ${ADDSSH[1]}>65536 ));then
echo "Port Error."
exit 1
fi
echo -n "请输入用户名 (Default root): "
read -r ADDSSH[2]
[ -z ${ADDSSH[2]} ] && ADDSSH[2]=root
echo -n "Pleast Input Host Aliasname: "
read -r ADDSSH[3]
[[ ${ADDSSH[0]} != 127.0.0.1 ]] && echo ${ADDSSH[@]} >>$LOCALFILE
return 5
}
i=0
while read LINE
do
var=(${LINE});((i++))
CONNECTING[$i]="$i '${var[0]}~${var[3]}'"
done <${LOCALFILE}
CONNECTING[0]="0 'ADD-SSH-HOST'"
#25,50,20 height of outer border,width,height of inner border
dialog \
--backtitle "SSH Mangemet" \
--menu "SSH Accounts ... " 25 50 20 \
--output-fd 3 \
${CONNECTING[@]} \
3>/dev/shm/ssh-connect-tmp
[[ $(cat /dev/shm/ssh-connect-tmp) == 0 ]] && ADDSSH_HOST
[ $? = 5 ] && exec ${0}
SSHVAR=($(cat -n $LOCALFILE|grep "^\ \+$(cat /dev/shm/ssh-connect-tmp)[^0-9]"))
if [[ $(cat /dev/shm/ssh-connect-tmp) > 0 ]];then
ssh -l ${SSHVAR[3]} -p ${SSHVAR[2]} ${SSHVAR[1]}
fi
rm -f /dev/shm/ssh-connect-tmp
exit 0
#############END#####################