-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqssh.sh
More file actions
268 lines (241 loc) · 8.38 KB
/
qssh.sh
File metadata and controls
268 lines (241 loc) · 8.38 KB
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
267
268
#!/data/data/com.termux/files/usr/bin/bash
set -euo pipefail
C_BOLD_BLUE="\033[1;34m"
C_BOLD_GREEN="\033[1;32m"
C_BOLD_YELLOW="\033[1;33m"
C_BOLD_RED="\033[1;31m"
C_BOLD_CYAN="\033[1;36m"
C_BOLD_MAGENTA="\033[1;35m"
C_BOLD_WHITE="\033[1;37m"
C_RESET="\033[0m"
CONFIG_FILE="$HOME/.ssh/qssh.ini"
BIN_PATH="$PREFIX/bin/qssh"
mkdir -p "$(dirname "$CONFIG_FILE")"
touch "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
err() { echo -e "${C_BOLD_RED}错误: $*${C_RESET}" >&2; }
info() { echo -e "${C_BOLD_GREEN}$*${C_RESET}"; }
warn() { echo -e "${C_BOLD_YELLOW}$*${C_RESET}"; }
check_dependencies() {
if ! command -v ssh &>/dev/null; then
warn "安装 openssh..."
pkg install -y openssh || { err "请手动安装 openssh"; exit 1; }
fi
if ! command -v sshpass &>/dev/null; then
warn "安装 sshpass..."
pkg install -y sshpass || { err "请手动安装 sshpass"; exit 1; }
fi
}
install_qssh() {
if [[ ! -f "$BIN_PATH" ]] || ! cmp -s "$0" "$BIN_PATH"; then
info "安装 qssh 到 $BIN_PATH"
cp "$0" "$BIN_PATH"
chmod +x "$BIN_PATH"
info "安装完成,可用 qssh 启动"
fi
}
ini_get() {
local target_section="$1" key="$2" line cur_section val in_section=0
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%$'\r'}"
if [[ "$line" =~ ^\[(.*)\]$ ]]; then
cur_section="${BASH_REMATCH[1]}"
[[ "$cur_section" == "$target_section" ]] && in_section=1 || in_section=0
continue
fi
if [[ $in_section -eq 1 && "$line" =~ ^[[:space:]]*$key[[:space:]]*=[[:space:]]*(.*)$ ]]; then
val="${BASH_REMATCH[1]}"
printf '%s' "$val"
return 0
fi
done < "$CONFIG_FILE"
return 1
}
ini_set_section() {
local section="$1" username="$2" hostname="$3" port="$4" auth_type="$5" auth_value="$6"
local tmp in_section=0 line cur_section line_orig
tmp="$(mktemp)"
while IFS= read -r line || [[ -n "$line" ]]; do
line_orig="$line"
line="${line%$'\r'}"
if [[ "$line" =~ ^\[(.*)\]$ ]]; then
cur_section="${BASH_REMATCH[1]}"
[[ "$cur_section" == "$section" ]] && in_section=1 || in_section=0
fi
[[ $in_section -eq 0 ]] && printf '%s\n' "$line_orig" >> "$tmp"
done < "$CONFIG_FILE"
{
printf '[%s]\n' "$section"
printf 'username = %s\n' "$username"
printf 'hostname = %s\n' "$hostname"
printf 'port = %s\n' "$port"
printf 'auth_type = %s\n' "$auth_type"
printf 'auth_value = %s\n\n' "$auth_value"
} >> "$tmp"
mv "$tmp" "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
}
ini_delete_section() {
local section="$1"
local tmp line cur_section in_section=0 line_orig
tmp="$(mktemp)"
while IFS= read -r line || [[ -n "$line" ]]; do
line_orig="$line"
line="${line%$'\r'}"
if [[ "$line" =~ ^\[(.*)\]$ ]]; then
cur_section="${BASH_REMATCH[1]}"
[[ "$cur_section" == "$section" ]] && in_section=1 || in_section=0
fi
[[ $in_section -eq 0 ]] && printf '%s\n' "$line_orig" >> "$tmp"
done < "$CONFIG_FILE"
mv "$tmp" "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
}
list_sections() {
awk '/^\[.*\]$/ { gsub(/\[|\]/,""); print $0 }' "$CONFIG_FILE"
}
print_menu() {
echo ""
echo -e "${C_BOLD_BLUE}====== SSH 快捷连接工具 ======${C_RESET}"
echo -e "${C_BOLD_GREEN}1. 添加配置${C_RESET}"
echo -e "${C_BOLD_CYAN}2. 连接服务器${C_RESET}"
echo -e "${C_BOLD_MAGENTA}3. 编辑配置${C_RESET}"
echo -e "${C_BOLD_RED}4. 删除配置${C_RESET}"
echo -e "${C_BOLD_BLUE}0. 退出${C_RESET}"
}
ensure_config_nonempty() {
[[ -z "$(list_sections)" ]] && { warn "当前没有任何 SSH 配置"; return 1; }
return 0
}
add_ssh_config() {
echo -e "${C_BOLD_BLUE}=> 添加新配置${C_RESET}"
while true; do
read -rp "配置名称: " section
[[ -z "$section" ]] && { err "配置名不能为空"; continue; }
if list_sections | grep -qx "$section"; then
err "配置名已存在,请重新输入"
continue
fi
break
done
read -rp "用户名 (默认 root): " username; username=${username:-root}
read -rp "主机: " hostname
[[ -z "$hostname" ]] && { err "主机不能为空"; return; }
read -rp "端口 (默认 22): " port; port=${port:-22}
read -rp "认证方式 密码(p)/密钥(k) [p/k]: " auth_choice
if [[ "$auth_choice" == "k" ]]; then
read -rp "私钥路径 (默认 ~/.ssh/id_rsa): " key_path
ini_set_section "$section" "$username" "$hostname" "$port" "key" "${key_path:-$HOME/.ssh/id_rsa}"
else
read -rsp "密码(不显示): " password; echo
ini_set_section "$section" "$username" "$hostname" "$port" "password" "$password"
fi
info "已保存配置 [$section]"
}
display_section_brief() {
local section="$1" u h p
u="$(ini_get "$section" "username")"
h="$(ini_get "$section" "hostname")"
p="$(ini_get "$section" "port")"
printf "[%s] %s@%s:%s\n" "$section" "$u" "$h" "$p"
}
SELECTED_SECTION=""
select_section() {
local prompt="${1:-输入编号:}" sections=()
while IFS= read -r s; do sections+=("$s"); done < <(list_sections)
[[ ${#sections[@]} -eq 0 ]] && { err "没有配置可选"; return 1; }
for i in "${!sections[@]}"; do
printf "%3d) " $((i+1))
display_section_brief "${sections[$i]}"
done
while true; do
read -rp "$prompt (回车/q返回): " idx
[[ -z "$idx" || "$idx" =~ ^[qQ]$ ]] && return 1
if [[ "$idx" =~ ^[0-9]+$ ]] && ((idx>=1 && idx<=${#sections[@]})); then
SELECTED_SECTION="${sections[$((idx-1))]}"
return 0
fi
err "无效输入"
done
}
ssh_connect_section() {
local section="$1"
local u h p t v
u=$(ini_get "$section" "username")
h=$(ini_get "$section" "hostname")
p=$(ini_get "$section" "port")
t=$(ini_get "$section" "auth_type")
v=$(ini_get "$section" "auth_value")
echo -e "${C_BOLD_BLUE}正在连接: ${C_BOLD_WHITE}$u@$h:$p${C_RESET}"
if [[ "$t" == "password" ]]; then
sshpass -p "$v" ssh -o StrictHostKeyChecking=no -p "$p" "$u@$h"
else
ssh -i "$v" -o StrictHostKeyChecking=no -p "$p" "$u@$h"
fi
}
connect_flow() {
ensure_config_nonempty || return 0
select_section "输入要连接的配置编号:" || return 0
ssh_connect_section "$SELECTED_SECTION"
}
edit_config() {
ensure_config_nonempty || return 0
select_section "输入要编辑的配置编号:" || return 0
local old="$SELECTED_SECTION"
local u h p t v
u=$(ini_get "$old" "username")
h=$(ini_get "$old" "hostname")
p=$(ini_get "$old" "port")
t=$(ini_get "$old" "auth_type")
v=$(ini_get "$old" "auth_value")
while true; do
read -rp "配置名称 [$old]: " new
new=${new:-$old}
if [[ "$new" != "$old" ]] && list_sections | grep -qx "$new"; then
err "配置名已存在,请重新输入"
continue
fi
break
done
read -rp "用户名 [$u]: " nu; nu=${nu:-$u}
read -rp "主机 [$h]: " nh; nh=${nh:-$h}
read -rp "端口 [$p]: " np; np=${np:-$p}
read -rp "认证类型 p/k [$t]: " at; at=${at:-$t}
local nv auth_type auth_value
if [[ "$at" == "k" ]]; then
read -rp "私钥路径 [$v]: " nv; nv=${nv:-$v}
auth_type="key"; auth_value="$nv"
else
read -rsp "密码(留空则不变): " pw; echo
auth_type="password"; auth_value="${pw:-$v}"
fi
if [[ "$new" != "$old" ]]; then
ini_delete_section "$old"
fi
ini_set_section "$new" "$nu" "$nh" "$np" "$auth_type" "$auth_value"
info "已更新配置 [$new]"
}
delete_ssh_config() {
ensure_config_nonempty || return 0
select_section "输入要删除的配置编号:" || return 0
local sec="$SELECTED_SECTION"
read -rp "确认删除 [$sec] ? (y/N): " yn
[[ "$yn" =~ ^[yY]$ ]] && { ini_delete_section "$sec"; info "已删除 $sec"; } || info "已取消"
}
main() {
check_dependencies
install_qssh
while true; do
print_menu
read -rp "请输入 (0-4): " choice
case "$choice" in
1) add_ssh_config ;;
2) connect_flow ;;
3) edit_config ;;
4) delete_ssh_config ;;
0) info "退出"; exit 0 ;;
*) warn "无效输入" ;;
esac
done
}
main