-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostfile.sh
209 lines (162 loc) · 5.59 KB
/
hostfile.sh
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
204
205
206
207
208
209
#!/bin/bash
source tools.sh
passwdsalt='ssssss'
function getIniFilePath() {
id=$1
echo "data/$1.ini"
}
function showConfigN() {
id=$1
inifile=$(getIniFilePath $id)
host=$(sed '/^host=/!d;s/^host=//' $inifile)
port=$(sed '/^port=/!d;s/^port=//' $inifile)
user=$(sed '/^user=/!d;s/^user=//' $inifile)
passwd=$(sed '/^passwd=/!d;s/^passwd=//' $inifile)
logintimes=$(sed '/^logintimes=/!d;s/^logintimes=//' $inifile)
desc=$(sed '/^desc=/!d;s/^desc=//' $inifile)
create_time=$(sed '/^create_time=/!d;s/^create_time=//' $inifile)
update_time=$(sed '/^update_time=/!d;s/^update_time=//' $inifile)
log "$id' '$host' '$port' '$user' '$passwd' '$logintimes' '$desc' '$create_time' '$update_time"
printf "\033[0;31m% 3s \033[m | %16s | %9s| %s\n" $id $host $logintimes $desc
}
#
direc=$(dirname $0)
function addHost() {
read -p '请输入主机Host: ' host
[[ -z $host ]] && echo '主机Host不能为空'
read -p '请输入主机端口: ' port
[[ -z $port ]] && echo '主机端口不能为空'
read -p '请输入登录用户名: ' user
[[ -z $user ]] && echo '登录用户不能为空'
read -s -p '请输入登录密码: ' passwd
[[ -z $passwd ]] && echo '登录密码不能为空'
echo
read -p '请输入主机描述: ' desc
[[ -z $host ]] && desc=$host
create_time=$(date '+%Y-%m-%d %H:%M:%S')
update_time=$(date '+%Y-%m-%d %H:%M:%S')
color blue '新增登录主机 '$user':'$passwd'@'$host':'$port' '$desc
read -p '是否新增登录主机? [y/n]' confirm
[[ $confirm != 'y' ]] && return
maxid=$(ls data/*.ini | cut -d/ -f2 | cut -d. -f1 | sort -nr | head -n1)
newId=$(($maxid + 1))
inifile=$(getIniFilePath $newId)
echo 'host='$host >$inifile
echo 'port='$port >>$inifile
echo 'user='$user >>$inifile
passwd=$(echo $passwdsalt$passwd|base64 -i)
echo 'passwd='$passwd >>$inifile
echo 'logintimes=0' >>$inifile
echo 'desc='$desc >>$inifile
echo 'create_time='$create_time >>$inifile
echo 'update_time='$update_time >>$inifile
}
function delHost() {
read -p '请输入主机ID: ' id
[[ -z $id ]] && echo '要删除主机id不能为空' && return
echo $id | grep -q '[^0-9]' >/dev/null && echo 'ID 必须是数字' && return
inifile=$(getIniFilePath $id)
mv $inifile $inifile'bk'
return
}
#$1 id
#$2 key
function listHost() {
echo "序号 | 主机 | 连接次数 | 说明"
id=$1
key=$2
#清空tmp
rm -f host.tmp
log 'id'$id
log 'key'$key
if [[ -n $id && $id > 0 ]]; then
log 'just to '$id
showConfigN $id
echo $id >>host.tmp
else
inifile=$(getIniFilePath '*')
if [[ -n $key ]]; then
log 'search'
for hostid in $(grep $key $inifile | cut -d/ -f2 | cut -d. -f1); do
showConfigN $hostid
echo $hostid >>host.tmp
done
else
log 'list all'$inifile
for hostitem in $(grep -H 'logintimes=' $inifile | sort -t '=' -k 2 -nr | cut -d/ -f2 | cut -d. -f1); do
log "list hostid:$hostitem"
showConfigN $hostitem
echo $hostitem >>host.tmp
done
fi
fi
}
function updateHost() {
read -p '请输入主机ID: ' id
[[ -z $id ]] && echo '主机id不能为空' && return
echo $id | grep -q '[^0-9]' >/dev/null && echo 'ID 必须是数字' && return
inifile=$(getIniFilePath $id)
[[ ! -f $inifile ]] && echo "主机ID不存在" && return
update_time=$(date '+%Y-%m-%d %H:%M:%S')
#更新
sed -i "/^update_time=/cupdate_time=$update_time" $inifile
read -p '请输入主机Host: ' host
#不为空更新
if [[ -n "$host" ]]; then
#更新
sed -i '/^host=/chost='$host $inifile
fi
read -p '请输入主机端口: ' port
if [[ -n "$port" ]]; then
#更新
sed -i '/^port=/cport='$port $inifile
fi
read -p '请输入登录用户名: ' user
if [[ -n "$user" ]]; then
#更新
sed -i '/^user=/cuser='$user $inifile
fi
read -p '请输入登录密码: ' passwd
if [[ -n "$passwd" ]]; then
#更新
passwd=$(echo $passwdsalt$passwd|base64 -i)
sed -i '/^passwd=/cpasswd='$passwd $inifile
fi
read -p '请输入主机描述: ' desc
if [[ -n "$desc" ]]; then
#更新
sed -i "/^desc=/cdesc=$desc" $inifile
fi
}
#jumpTo $id $host $port $user $passwd $logintimes
function jumpTo() {
id=$1
inifile=$(getIniFilePath $id)
host=$(sed '/^host=/!d;s/^host=//' $inifile)
port=$(sed '/^port=/!d;s/^port=//' $inifile)
user=$(sed '/^user=/!d;s/^user=//' $inifile)
passwd=$(sed '/^passwd=/!d;s/^passwd=//' $inifile)
logintimes=$(sed '/^logintimes=/!d;s/^logintimes=//' $inifile)
desc=$(sed '/^desc=/!d;s/^desc=//' $inifile)
create_time=$(sed '/^create_time=/!d;s/^create_time=//' $inifile)
update_time=$(sed '/^update_time=/!d;s/^update_time=//' $inifile)
log "$id' '$host' '$port' '$user' '$passwd' '$logintimes' '$desc' '$create_time' '$update_time"
newlogintimes=$(($logintimes + 1))
#更新登录次数
sed -i 's/logintimes='$logintimes'/logintimes='$newlogintimes'/' $inifile
#看passwd是不是pem文件,如果是直接使用pem登录,否则当做字符串密码登录
echo $passwd | grep -q ".pem$"
RETURN=$?
if [[ $RETURN == 0 ]]; then
echo "ssh -i $direc/$passwd $user@$host -p $port"
ssh -i $direc/keys/$passwd $user@$host -p $port
else
echo "expect -f $direc/ssh_login.exp $host $user $passwd $port"
passwd=$(echo $passwd|base64 -d)
passwd=$(echo ${passwd#$passwdsalt})
echo "expect -f $direc/ssh_login.exp $host $user $port"
expect -f $direc/ssh_login.exp $host $user $passwd $port
fi
}
#up text passwd to base64 code with passwdsalt head:
#sed -i '/^passwd=/cpasswd='$(echo ssssss$(sed '/^passwd=/!d;s/passwd=//' 2.ini)|base64 -i) 2.ini