autossh
Auto Login SSH Server (expect-based)
用于自动登陆,基于expect
Install
$ git clone https://github.com/wufeifei/autossh.git
$ sudo cp autossh/autossh /usr/local/bin/
Config
$ cat ~/.autosshrc
server name|192.168.1.110|root|password|port|is_bastion
wufeifei|192.168.1.1|root|password|22|1
Usage
$ autossh
############################################################
# [AUTO SSH] #
# #
# #
# [1] 192.168.1.110:feei #
# [2] 10.11.2.103:root #
# [3] 103.21.140.84:root #
# #
# #
############################################################
Server Number:(Input Server Num)
OR
$ autossh 1
OR Auto Sudo
$ autossh 3 sudo
OR Bastion Host
$ autossh 1 10.12.0.123
OR Auto Sudo With Bastion
$ autossh 1 10.11.0.123 sudo
Shell Script
#!/bin/bash
# AUTO SSH - Auto Login SSH Server (expect-based)
#
# @category Main
# @package AutoSSH
# @author Feei <wufeifei@wufeifei.com>
# @license Copyright (C) 2015 Feei. All Rights Reserved
# @link http://github.com/wufeifei/autossh
# @install
#
# $ git clone https://github.com/wufeifei/autossh.git
# $ sudo cp autossh/autossh /usr/local/bin/
# $ 'server name|192.168.1.1|root|password|22|0' > ~/.autosshrc
#
# @usage
# $ autossh // List Server
# $ autossh 1 // Login Num n Server
AUTO_SSH_CONFIG=`cat ~/.autosshrc`
BORDER_LINE="\033[1;31m############################################################ \033[0m"
echo -e $BORDER_LINE
echo -e "\033[1;31m# [AUTO SSH] # \033[0m"
echo -e "\033[1;31m# # \033[0m"
echo -e "\033[1;31m# # \033[0m"
i=0;
if [ "$AUTO_SSH_CONFIG" == "" ]; then
echo -e "\033[1;31m# Config(~/.autosshrc) Not Found # \033[0m";
echo -e "\033[1;31m# # \033[0m"
echo -e "\033[1;31m# # \033[0m"
echo -e $BORDER_LINE
else
for server in $AUTO_SSH_CONFIG; do
i=`expr $i + 1`
SERVER=`echo $server | awk -F\| '{ print $1 }'`
IP=`echo $server | awk -F\| '{ print $2 }'`
NAME=`echo $server | awk -F\| '{ print $3 }'`
LINE="\033[1;31m#"\ [$i]\ $SERVER\ -\ $IP':'$NAME
MAX_LINE_LENGTH=`expr ${#BORDER_LINE}`
CURRENT_LINE_LENGTH=`expr "${#LINE}"`
DIS_LINE_LENGTH=`expr $MAX_LINE_LENGTH - $CURRENT_LINE_LENGTH - 9`
echo -e $LINE"\c"
for n in $(seq $DIS_LINE_LENGTH);
do
echo -e " \c"
done
echo -e "# \033[0m"
done
echo -e "\033[1;31m# # \033[0m"
echo -e "\033[1;31m# # \033[0m"
echo -e $BORDER_LINE
# GET INPUT CHOSEN OR GET PARAM
if [ "$1" != "" ]; then
no=$1
else
no=0
until [ $no -gt 0 -a $no -le $i ] 2>/dev/null
do
echo -e 'Server Number:\c'
read no
done
fi
fi
i=0
for server in $AUTO_SSH_CONFIG; do
i=`expr $i + 1`
if [ $i -eq $no ] ; then
IP=`echo $server | awk -F\| '{ print $2 }'`
NAME=`echo $server | awk -F\| '{ print $3 }'`
PASS=`echo $server | awk -F\| '{ print $4 }'`
PORT=`echo $server | awk -F\| '{ print $5 }'`
ISBASTION=`echo $server | awk -F\| '{ print $6 }'`
FILE='/tmp/.login.sh'
if [ "$PORT" == "" ]; then
PORT=10022
fi
echo '#!/usr/bin/expect -f' > $FILE
echo 'set timeout 30' >> $FILE
echo "spawn ssh -p$PORT -l "$NAME $IP >> $FILE
if [ "$PASS" != "" ]; then
echo 'expect "password:"' >> $FILE
echo 'send '$PASS"\r" >> $FILE
if [ "$2" == 'sudo' ]; then
echo 'expect "@"' >> $FILE
echo 'send "sudo su\r"' >> $FILE
echo 'expect "password for"' >> $FILE
echo 'send '$PASS"\r" >> $FILE
else
if [ "$ISBASTION" == 1 ] && [ "$2" != "" ]; then
echo 'expect ">"' >> $FILE
echo 'send '$2"\r" >> $FILE
echo 'expect "password:"' >> $FILE
echo 'send '$PASS"\r" >> $FILE
if [ "$3" == 'sudo' ]; then
echo 'expect "@"' >> $FILE
echo 'send "sudo su\r"' >> $FILE
echo 'expect "password for"' >> $FILE
echo 'send '$PASS"\r" >> $FILE
fi
fi
fi
fi
echo 'interact' >> $FILE
chmod a+x $FILE
$FILE
echo '' > $FILE
break;
fi
done