-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautossh
112 lines (107 loc) · 3.52 KB
/
autossh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# AUTO SSH - Auto Login SSH Server (expect-based)
#
# @category Main
# @package AutoSSH
# @author Feei <[email protected]>
# @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