-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlucy
174 lines (156 loc) · 4.28 KB
/
lucy
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/local/bin/bash
AUTO_LOGIN_DIR=~/ssh-lucy
function init(){
if [ -f "${AUTO_LOGIN_DIR}/servers" ];then
echo "已经初始化过! 无需再次初始化!" && exit 1
else
if [ ! -d $AUTO_LOGIN_DIR ]; then
mkdir ~/ssh-lucy
fi
touch $AUTO_LOGIN_DIR/servers
echo "初始化成功! 请加入服务器信息. 🙂" && exit 1
fi
}
declare -A server_list=()
if [ -f "$AUTO_LOGIN_DIR/servers" ];then
while read ip user password port name ;do
server_list["${name}"]="${ip} ${user} ${password} ${port}"
done < "$AUTO_LOGIN_DIR/servers"
else
init
fi
# 查询列表,不带密码
function list() {
echo -e "Ip\t\t\tPort\tUser\t\tName"
for key in ${!server_list[@]}; do
servers=(${server_list[$key]})
echo -e "${servers[0]}\t\t${servers[3]}\t${servers[1]}\t\t${key}"| toilet -f term -F border -t --gay
done
exit 0
}
# 查询列表,带密码
function listPwd() {
echo -e "Ip\t\t\tPort\tUser\t\tPassword\t\t\tName"
for key in ${!server_list[@]}; do
servers=(${server_list[$key]})
echo -e "${servers[0]}\t\t${servers[3]}\t${servers[1]}\t\t${servers[2]}\t\t\t${key}"| toilet -f term -F border -t --gay
done
exit 0
}
# 展示可用的命令
function usage() {
echo "usage:"
echo "ssha [-h] [-l] [-p] [-f] [-a] [-s <server alias>] [-d <server alias>]"
exit 0
}
# 登录服务器
function login(){
server=(${server_list[$1]})
if [ ! $server ]; then
echo "找不到服务" && list && exit 1
fi
echo "$USER logging into the【${server[0]}】 server"| toilet -f term -F border --gay
export PASSWORD=${server[2]};
command="
expect {
\"*assword\" {set timeout 6000; send \$env(PASSWORD)\n; exp_continue ; sleep 3; }
\"*passphrase\" {set timeout 6000; send \$env(PASSWORD)\r\n\"; exp_continue ; sleep 3; }
\"yes/no\" {send \"yes\n\"; exp_continue;}
\"Last*\" { send_user \"\nsuccessfully logined 【${server[0]}】\n\";}
}
interact
";
pem=${server[2]}
if [ -f "$pem" ]
then
expect -c "
spawn ssh -p ${server[3]} -i ${server[2]} ${server[1]}@${server[0]}
${command}
"
else
expect -c "
spawn ssh -p ${server[3]} ${server[1]}@${server[0]}
${command}
"
fi
echo -e "\t$USER logged out the【${server[0]}】 server\t" | toilet -f term -F border --gay
}
# 添加服务器节点
function add(){
read -p "Please enter servername: " servername
servers=(${server_list[$servername]})
if [ $servers ]; then
echo "该服务已存在, 请更换服务名称. "
echo -e "Ip\t\t\tPort\tUser\t\tName"
echo -e "${servers[0]}\t${servers[3]}\t${servers[1]}\t${servername}"| toilet -f term -F border -t --gay && exit 1
fi
read -p "Please enter ip: " ip
read -p "Please enter username: " username
read -p "Please enter password: " password
read -p "Please enter port: " port
echo -e "${ip}\t\t${username}\t\t${password}\t\t${port}\t\t${servername}" >> $AUTO_LOGIN_DIR/servers
}
# 查找servers文件目录
function findDir() {
echo $AUTO_LOGIN_DIR
}
# 删除服务器节点
function delete() {
server=(${server_list[$1]})
if [ ! $server ]; then
echo "删除的节点不存在" && exit 1
fi
read -p "是否确认删除该服务器节点[$1]: y/n ?" answer
if [ $answer = 'y' ]; then
echo '确认删除'
unset server_list[$1]
num=1
if [ -f "$AUTO_LOGIN_DIR/servers" ];then
while read ip user password port name ;do
if [ $name = $1 ]; then
break;
fi
num=$(($num+1))
done < "$AUTO_LOGIN_DIR/servers"
fi
sed -i -e "$num"d $AUTO_LOGIN_DIR/servers && echo "删除成功~" && exit 1
elif [ $answer = 'n' ]; then
echo '取消删除'
else
echo '请输入正确命令: [y/n] '
fi
}
# clear screen
clear
while getopts hlpaifs:d: ARGS
do
case $ARGS in
s)
login $OPTARG
;;
d)
delete $OPTARG
;;
a)
add
;;
l)
list
;;
f)
findDir
;;
p)
listPwd
;;
i)
init
;;
h)
usage
;;
*)
usage
;;
esac
done