-
Notifications
You must be signed in to change notification settings - Fork 0
/
shm.sh
266 lines (241 loc) · 7.32 KB
/
shm.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
help() {
echo "$0"
echo "$0 ls"
echo "$0 add nickname user@example.com 22 password"
echo "$0 rm nickname "
echo "$0 cp nickname:/tmp/example.txt ."
echo "$0 cp example.txt nickname:/tmp"
}
select_option() {
clear
echo -e "Enter $0 help for help\n"
choices=("$@") # 将选项数组声明为全局变量
selected=0 # 初始化选择索引
num_choices=${#choices[@]}
tput civis # 隐藏光标
trap "tput cnorm; exit" EXIT # 在脚本退出时恢复光标并退出
# 记录当前光标位置
tput sc # 保存当前光标位置
while true; do
# 移动光标到记录的位置
tput rc
# 清除之前的内容
for i in $(seq 0 $((num_choices - 1))); do
tput el # 清除到行尾
done
# 重新输出选项
for index in "${!choices[@]}"; do
if [ $index -eq $selected ]; then
printf "\033[31m> ${choices[$index]}\033[0m\n" # 高亮显示选中的选项
else
echo " ${choices[$index]}"
fi
done
read -n1 -s key # 读取单个按键并保持输入的隐私
case "$key" in
A) # 上箭头
if [ $selected -gt 0 ]; then
selected=$((selected - 1))
tput cuu 1 # 光标上移1行
fi
;;
B) # 下箭头
if [ $selected -lt $(( num_choices - 1 )) ]; then
selected=$((selected + 1))
tput cud 1 # 光标下移1行
fi
;;
"") # 回车键
break
;;
esac
done
tput cnorm # 恢复光标
# 打印最终结果日志
selected_option="${choices[$selected]}"
}
# pre check util
commands=("xxd" "base64" "expect" "ssh" "scp")
for cmd in "${commands[@]}"; do
if ! which "$cmd" >/dev/null 2>&1; then
echo "Error: $cmd command is not available."
exit 1
fi
done
# init
mkdir -p $HOME/.config/
touch $HOME/.config/.shm
# read db
data=()
while IFS= read -r line; do
data+=("$line")
done < "$HOME/.config/.shm"
length=${#data[@]}
# main
case "$#" in
0)
# list all
if ["${#data[@]}" = 0]; then
exit 3
fi
options=()
for (( i=0; i<$length; i++ )); do
read -r -a each <<< "${data[$i]}"
line="$i"
for (( j=0; j<${#each[@]}-1; j++));do
line+=" ${each[$j]}"
done
options+=("$line")
done
select_option "${options[@]}"
id=$(("${selected_option%% *}")) # get id
for (( i=0; i<=id; i++)); do
read -r -a each <<< "${data[$i]}"
USER_HOST="${each[1]}"
PORT="${each[2]}"
PASSWORD="`echo ${each[3]} | base64 -d | xxd -r -p`"
expect -c "
set user_host \"$USER_HOST\"
set port \"$PORT\"
log_user 0
spawn ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no -p \$port \$user_host
log_user 1
expect {
\"password:\" {
send \"$PASSWORD\r\"
}
# 处理连接问题
\"Permission denied\" {
puts \"Permission denied. Please check your credentials.\"
exit 1
}
\"Could not resolve hostname\" {
puts \"Could not resolve hostname. Please check the host address.\"
exit 1
}
}
interact
"
done
;;
1)
# ssh list
if [ "$1" = "help" ]; then
help
exit 0
fi
if [ "$1" != "ls" ]; then
echo "Use correct operater"
exit 2
fi
for (( i=0; i<$length; i++ )); do
read -r -a each <<< "${data[$i]}"
line="$i"
for (( j=0; j<${#each[@]}-1; j++));do
line+="\t${each[$j]}"
done
echo -e "$line"
done
;;
2)
# delete target
if [ "$1" != "rm" ]; then
echo "Use correct operater"
exit 2
fi
echo -n > $HOME/.config/.shm # clear the file
for (( i=0; i<$length; i++ )); do
read -r -a each <<< "${data[$i]}"
if [ "$2" = "${each[0]}" ]; then
continue
fi
echo "${data[$i]}" >> $HOME/.config/.shm
done
;;
3)
# copy file
# echo "$0 cp nickname:/tmp/example.txt ."
# echo "$0 cp example.txt nickname:/tmp"
if [ "$1" != "cp" ]; then
echo "Use correct operater"
exit 2
fi
first_path=$2
second_path=$3
if [[ "$first_path" == *:* ]]; then
nickname="${first_path%%:*}"
elif [[ "$second_path" == *:* ]]; then
nickname="${second_path%%:*}"
else
echo "Use correct file path"
exit 5
fi
for (( i=0; i<$length; i++ )); do
read -r -a each <<< "${data[$i]}"
if [ "$nickname" = "${each[0]}" ]; then
if [[ "$first_path" == *:* ]]; then
first_path="${first_path//$nickname/${each[1]}}"
elif [[ "$second_path" == *:* ]]; then
second_path="${second_path//$nickname/${each[1]}}"
fi
PORT="${each[2]}"
PASSWORD="`echo ${each[3]} | base64 -d | xxd -r -p`"
expect -c "
set port \"$PORT\"
set first \"$first_path\"
set second \"$second_path\"
log_user 0
spawn scp -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no -r -P \$port \$first \$second
log_user 1
expect {
\"password:\" {
send \"$PASSWORD\r\"
}
# 处理连接问题
\"Permission denied\" {
puts \"Permission denied. Please check your credentials.\"
exit 1
}
\"Could not resolve hostname\" {
puts \"Could not resolve hostname. Please check the host address.\"
exit 1
}
}
interact
"
break
fi
done
# first_path second_path replace user_host
# scp -r -P 22 -o StrictHostKeyChecking=no
# echo "cp file to"
;;
5)
# add target
# shm add xzmu bash@xzmu.freet.tech 22 123456
if [ "$1" != "add" ]; then
echo "Use correct operater"
exit 2
fi
name="$2"
user_host="$3"
port="$4"
password="$5"
# check repeat
for (( i=0; i<$length; i++ )); do
read -r -a each <<< "${data[$i]}"
if [ "$2" = "${each[0]}" ]; then
echo "Name $2 has existed!"
exit 3
fi
done
passwd="`echo -n "$password" | xxd -p | base64`"
res="$name $user_host $port $passwd"
echo "$res" >> $HOME/.config/.shm
;;
*)
help
exit 3
;;
esac