-
Notifications
You must be signed in to change notification settings - Fork 5
/
zssh
executable file
·205 lines (179 loc) · 5.39 KB
/
zssh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# 通过ssh自动登录到服务器,在项目根目录下输入
# ./auto-ssh 10.65.215.31 func57 func57
file_dir="$HOME/.autossh"
history_file="auto-ssh.txt"
if [ ! -d $file_dir ];then
mkdir $file_dir
fi
#如果文件不存在,创建文件
if [ ! -f "$file_dir/$history_file" ];then
#echo "创建文件 $file_dir/$history_file"
touch "$file_dir/$history_file"
fi
from_record=false
ip=$1
username=$2
password=$3
desc=$4
#输入的host是否存在于文件中
is_saved_host() {
#test -f "$file_dir/$history_file" && (cat "$file_dir/$history_file" | grep $1 > /dev/null)
if [ -f "$file_dir/$history_file" ];then
while read line
do
saved_host=$(echo $line | awk '{print $1}')
if [ $saved_host == $1 ];then
return 0
fi
done < "$file_dir/$history_file"
fi
return 1
}
ssh_login() {
# which expect > /dev/null 2>&1
# if [ $? == 0 ]; then
# expect_ssh_login () {
# expect -c "set timeout -1;
# spawn ssh -o StrictHostKeyChecking=no $2 ${@:3};
# expect {
# *assword:* {send -- $1\r;
# expect {
# *denied* {exit 2;}
# eof
# }
# }
# eof {exit 1;}
# }
# "
# return $?
# }
# echo ">>> expect"
# expect_ssh_login $3 $2@$1
# else
which sshpass > /dev/null 2>&1
if [ $? == 1 ]; then
origin_dir=$(pwd)
tmp_path='tmp'
mkdir $tmp_path
cd $tmp_path
wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz
tar xvzf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure --prefix=/usr/local/Cellar/sshpass/1.05
make
echo "===开始安装sshpass"
sudo make install
ln -s /usr/local/Cellar/sshpass/1.05/bin/sshpass /usr/local/bin/sshpass
cd $origin_dir
rm -rf "${origin_dir}/${tmp_path}"
which sshpass > /dev/null 2>&1
if [ $? == 0 ]; then
echo "===sshpass 安装成功"
else
echo "===sshpass 安装失败"
exit 1
fi
fi
echo ">>> sshpass"
sshpass -p $3 ssh $2@$1
#fi
}
#保存host信息到文件中 host username password desc
save_or_update_host() {
#组装记录
record="$1 $2 $3"
if [ $# != 4 ];then
record="$record $1"
else
record="$record $4"
fi
is_saved_host $1
if [ $? != 0 ] ;then
#如果这个host没有被保存,就保存到文件中
echo "$record >> $file_dir/$history_file"
echo $record >> "$file_dir/$history_file"
else
#如果这个host已经被保存,更新数据
tmp_file="$file_dir/auto-ssh-bak"
rm $tmp_file > /dev/null 2>&1
touch $tmp_file
while read line
do
saved_host=$(echo $line | awk '{print $1}')
if [ $saved_host == $1 ];then
echo $record >> $tmp_file
else
echo $line >> $tmp_file
fi
done < "$file_dir/$history_file"
cat $tmp_file > "$file_dir/$history_file"
rm $tmp_file > /dev/null 2>&1
fi
}
if [ $# -lt 3 ]
then
record_size=$(cat "$file_dir/$history_file" | wc -l)
record_size=$(echo $record_size)
if [ $record_size == 0 ];then
echo "<< 参数不正确 $1 $2 $3"
echo "$0 ip username password"
exit 1
fi
echo "Which server you want to connect?"
cat -n "$file_dir/$history_file"
if [ $record_size == 1 ];then
read -p "Input 1 to connect: " num
else
read -p "[1,$record_size]: " num
fi
until [ $num -gt 0 ] 2>/dev/null && [ $num -le $record_size ] 2>/dev/null
do
echo -n "--warn wrong input"
echo ''
if [ $record_size == 1 ];then
read -p "Input 1 to connect: " num
else
read -p "[1,$record_size]: " num
fi
done
record=$(sed -n "${num}P" "$file_dir/$history_file")
from_record=true
ip=$(echo $record | awk '{print $1}')
username=$(echo $record | awk '{print $2}')
password=$(echo $record | awk '{print $3}')
desc=$(echo $record | awk '{print $4}')
else
is_saved_host $ip
if [ $? != 0 ] ;then
echo "====================="
echo "host : $ip"
echo "username: $username"
echo "password: $password"
echo "====================="
echo "Do you want to save the connection information?"
while read -p "y/n: " line
do
case $line in
YES | yes | Y | y)
if [ $desc == "" ];then
echo "没有desc"
$desc=$ip
fi
save_or_update_host $ip $username $password $desc
break
;;
NO | no | N | n)
break
;;
*)
#"default"
;;
esac
done
else
save_or_update_host $ip $username $password $desc
fi
fi
#echo $ip $username $password $desc
ssh_login $ip $username $password