forked from Zxce3/TemSSH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuissh.sh
403 lines (373 loc) · 15.8 KB
/
tuissh.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# Use this to show help information on how to use the script.
helptext () {
dialog --backtitle "TUI SSH Manager" --title "HELP" --msgbox \
".----------------------------------------------o\n
| The MENU options are: |\n
| - 1) Adding new SSH server. |\n
| - 2) Show all saved server(s). |\n
| - 3) Connect to SSH server. |\n
| - 4) Delete server. |\n
| - 5) Send data via SCP. |\n
| - 6) Connect to SSH, use X11 forwarding. |\n
| - 7) Exit from program. |\n
|----------------------------------------------|\n
| You car run the script with |\n
| the '-h' or '--help' flag to see this. |\n
|----------------------------------------------|\n
| You can use the arrow keys, |\n
| tab and enter to use the interface. |\n
*----------------------------------------------o\n" 20 55
printf '\033[2J\033[3J\033[1;1H'
dialog --backtitle "TUI SSH Manager" --title "HELP" --yesno "Do you want to continue to the MENU?" 5 55
tmv=$?
printf '\033[2J\033[3J\033[1;1H'
if [ $tmv -eq 1 ]; then exit 0; fi
}
# Check for the help flags on start.
if [[ $1 == "-h" || $1 == "--help" ]];then
helptext
fi
# Check for the existence of the config file, if not create it.
if [ ! -f ~/.config/termssh-manager.conf ]; then
# Configuration file not found, create new file...
configmsgbox="Configuration file not found, without one being created the program will close.\n\nDo you wish to create it now?"
dialog --backtitle "TUI SSH Manager" --title "No configuration file found!" --yesno "$configmsgbox" 8 55
filecreator=$?
if [ $filecreator -eq 0 ]; then
dialog --backtitle "TUI SSH Manager" --title "Configuration file created." --msgbox "Config file is created ~/.config/termssh-manager.conf" 6 35
echo "declare -A SSH_CONFIGS" > ~/.config/termssh-manager.conf
else
dialog --backtitle "TUI SSH Manager" --title "Configuration file has NOT been created." --msgbox "\nThe program will close." 6 55
exit 0
fi
fi
# Load the config file
source ~/.config/termssh-manager.conf
# Add new server to use for SSH and SCP connection
configure_ssh() {
server_name=""
SSH_HOST=""
SSH_PORT=" "
SSH_USER=""
IFS=$'\n' read -r -d '' server_name SSH_HOST SSH_PORT SSH_USER < <(dialog --ok-label "Submit" --backtitle "TUI SSH Manager" --title "Configure SSH Server" --form "\nAdd new server:" 25 60 16 "Server name:" 1 1 "$server_name" 1 25 25 30 "Host/IP addreas:" 2 1 "$SSH_HOST" 2 25 25 30 "Port (optional):" 3 1 "$SSH_PORT" 3 25 25 30 "Username:" 4 1 "$SSH_USER" 4 25 25 30 3>&1 1>&2 2>&3 3>&-)
if [ -z "$server_name$SSH_HOST$SSH_USER" ]; then return 0; fi
if [[ "$SSH_PORT" == " " ]]; then SSH_PORT=""; fi
if [ -z $server_name ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Server name cannot be empty!" 6 35 && return 1; fi
# If the server's name exists abort
for server in "${!SSH_CONFIGS[@]}"; do
if [[ $server_name = $server ]]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "This name already in existance, \nuse an other one or \ndelete the existing one!" 6 35 && return 1; fi
done
if [ -z $SSH_HOST ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Host/IP cannot be empty!" 6 35 && return 1; fi
if [ -z $SSH_USER ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Username cannot be empty!" 6 35 && return 1; fi
if [ -z "$SSH_PORT" ];then
SSH_CONFIGS[$server_name]="$SSH_USER@$SSH_HOST"
else
SSH_CONFIGS[$server_name]="$SSH_USER@$SSH_HOST -p $SSH_PORT"
fi
echo "declare -A SSH_CONFIGS" > ~/.config/termssh-manager.conf
for key in "${!SSH_CONFIGS[@]}"; do
echo "SSH_CONFIGS[$key]='${SSH_CONFIGS[$key]}'" >> ~/.config/termssh-manager.conf
done
dialog --backtitle "TUI SSH Manager" --title "Server added" --msgbox "Config saved!" 5 35
}
# Show the contents of the config file
show_config() {
showbox=""
scounter=0
maxname_len=0
for server in "${!SSH_CONFIGS[@]}"; do
if [ $maxname_len -lt ${#server} ]; then maxname_len=${#server}; fi
done
showbox="List SSH config:\n"
for server in "${!SSH_CONFIGS[@]}"; do
showbox=$showbox"Server: $server"
COUNTER=$((maxname_len - ${#server}))
until [ $COUNTER -eq "0" ]; do
showbox=$showbox" "
let COUNTER-=1
done
showbox=$showbox" ($scounter)\n"
scounter=$((scounter + 1))
temp_var=${SSH_CONFIGS[$server]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
showbox=$showbox"Config: $cutservername\n"
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
showbox=$showbox" on port: $serverport"
fi
showbox=$showbox"\n"
done
showbox=$showbox"End of list.\n"
dialog --backtitle "TUI SSH Manager" --title "Saved server(s)" --msgbox "$showbox" 18 55
}
# Connect to server via SSH
connect_ssh() {
connectbox=""
scounter=0
stcounter=0
maxname_len=0
for server in "${!SSH_CONFIGS[@]}"; do
if [ $maxname_len -lt ${#server} ]; then maxname_len=${#server}; fi
done
for server in "${!SSH_CONFIGS[@]}"; do
connectbox="$connectbox$server"
COUNTER=$((maxname_len - ${#server}))
until [ $COUNTER -eq 0 ]; do
connectbox=$connectbox" "
let COUNTER-=1
done
connectbox=$connectbox" --- ($scounter)\n"
scounter=$((scounter + 1))
done
server_name=$(dialog --backtitle "TUI SSH Manager" --title "Connect to server via SSH" --inputbox "Available server(s):\n$connectbox\n\nSelect server:" 18 55 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then return 0; fi
if [ -z $server_name ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && return 1; fi
# If the input is a number
if [[ "$server_name" =~ ^[0-9]+$ ]]; then
for server in "${!SSH_CONFIGS[@]}"; do
if [ $stcounter -eq $server_name ]; then server_name=$server && break; fi
stcounter=$((stcounter + 1))
done
fi
if [[ ! ${SSH_CONFIGS[$server_name]+_} ]]; then
dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Server Not found." 6 35
else
temp_var=${SSH_CONFIGS[$server_name]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
connectbox="Connecting to $cutservername"
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
connectbox=$connectbox" on port: $serverport"
fi
connectbox=$connectbox"...\n"
connectbox=$connectbox"Are you sure you want to connect?"
dialog --backtitle "TUI SSH Manager" --title "Connecting..." --yesno "$connectbox" 8 55
confirm=$?
if [[ $confirm -eq 0 ]]; then
printf '\033[2J\033[3J\033[1;1H'
ssh ${SSH_CONFIGS[$server_name]}
fi
fi
}
# Send file to server via SCP
fileshare_scp() {
scpbox=""
isSendDir=0 # 0 == false
# dialog --backtitle "TUI SSH Manager" --title "SCP Fileshare" --yesno "Do you want to send a file?" 6 35
# isSendDir=$?
scounter=0
stcounter=0
maxname_len=0
for server in "${!SSH_CONFIGS[@]}"; do
if [ $maxname_len -lt ${#server} ]; then maxname_len=${#server}; fi
done
for server in "${!SSH_CONFIGS[@]}"; do
scpbox=$scpbox"$server"
COUNTER=$((maxname_len - ${#server}))
until [ $COUNTER -eq 0 ]; do
scpbox=$scpbox" "
let COUNTER-=1
done
scpbox=$scpbox" --- ($scounter)\n"
scounter=$((scounter + 1))
done # Available server(s) for SCP:
server_name=$(dialog --backtitle "TUI SSH Manager" --title "SCP Fileshare" --inputbox "Select server:\n$scpbox" 18 55 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then return 0; fi
if [ -z $server_name ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && return 1; fi
# If the input is a number
if [[ "$server_name" =~ ^[0-9]+$ ]]; then
for server in "${!SSH_CONFIGS[@]}"; do
if [ $stcounter -eq $server_name ]; then server_name=$server && break; fi
stcounter=$((stcounter + 1))
done
fi
dirText="error"
if [[ ! ${SSH_CONFIGS[$server_name]+_} ]]; then
dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Server Not found." 6 35
else
# Get paths to the files/directories
fsend=$(dialog --backtitle "TUI SSH Manager" --stdout --title "Please choose a file or directory" --fselect $HOME/ 12 60)
if [ $? -eq 1 ]; then return 0; fi
if [ ${fsend:0:1} == '~' ]; then # not needed for TUI
fsend=$HOME${fsend:1}
fi
if [[ -d $fsend ]]; then
# directory
isSendDir=1
dirText="directory"
elif [[ -f $fsend ]]; then
# file
isSendDir=0
dirText="file"
else
# invalid
dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "The given PATH to file is INVALID!" 6 35
return 1
fi
freceive=$(dialog --backtitle "TUI SSH Manager" --title "Path to the location to receive" --inputbox "Sending $fsend [$dirText] to:" 11 55 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then return 0; fi
if [ -z $freceive ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && return 1; fi
# Connecting to the server to send data
temp_var=${SSH_CONFIGS[$server_name]}
scpbox=""
cutservername=$(echo $temp_var | cut -d' ' -f 1)
scpbox=$scpbox"Connecting to $cutservername"
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
scpbox=$scpbox" on port: [$serverport]"
fi
scpbox=$scpbox"\n"
scpbox=$scpbox"Sending the $fsend [$dirText] \n to $freceive\n"
scpbox=$scpbox"Are you sure you want to continue to send?"
dialog --backtitle "TUI SSH Manager" --title "SCP Fileshare" --yesno "$scpbox" 13 55 3>&1 1>&2 2>&3 3>&-
confirm=$?
if [ -z $confirm ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && confirm="n"; fi
if [ $confirm -eq 0 ]; then
if [ $isSendDir -eq 1 ]; then
temp_var=${SSH_CONFIGS[$server_name]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
printf '\033[2J\033[3J\033[1;1H'
scp -P $serverport -r $fsend $cutservername:$freceive
sleep 2
printf '\033[2J\033[3J\033[1;1H'
else
printf '\033[2J\033[3J\033[1;1H'
scp -r $fsend ${SSH_CONFIGS[$server_name]}:$freceive
sleep 2
printf '\033[2J\033[3J\033[1;1H'
fi
else
temp_var=${SSH_CONFIGS[$server_name]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
printf '\033[2J\033[3J\033[1;1H'
scp -P $serverport $fsend $cutservername:$freceive
sleep 2
printf '\033[2J\033[3J\033[1;1H'
else
printf '\033[2J\033[3J\033[1;1H'
scp $fsend ${SSH_CONFIGS[$server_name]}:$freceive
sleep 2
printf '\033[2J\033[3J\033[1;1H'
fi
fi
else
dialog --backtitle "TUI SSH Manager" --title "SCP Fileshare" --msgbox "Cancel sending." 6 35
fi
fi
}
# Delete a server from config
delete_server() {
rmbox=""
dans=""
for server in "${!SSH_CONFIGS[@]}"; do
rmbox="$rmbox\n$server"
done
server_name=$(dialog --backtitle "TUI SSH Manager" --title "Select the server you want to delete" --inputbox "Available server(s):\n$rmbox\n\nSelect server:" 15 55 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then return 0; fi
if [ -z $server_name ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && return 1; fi
if [[ ! ${SSH_CONFIGS[$server_name]+_} ]]; then
dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Server not found." 6 35
else
temp_var=${SSH_CONFIGS[$server_name]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
confirmbox="Are you sure you want to delete\n$cutservername"
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
confirmbox=$confirmbox" port: [$serverport]"
fi
confirmbox=$confirmbox"?"
dialog --backtitle "TUI SSH Manager" --title "Deleting server" --yesno "$confirmbox" 7 55
if [ $? -eq 0 ];then
dans="yes"
else
dans="no"
fi
if [[ $dans = [Yy] || $dans = [Yy][Ee][Ss] ]]; then
unset SSH_CONFIGS[$server_name]
echo "declare -A SSH_CONFIGS" > ~/.config/termssh-manager.conf
for key in "${!SSH_CONFIGS[@]}"; do
echo "SSH_CONFIGS[$key]='${SSH_CONFIGS[$key]}'" >> ~/.config/termssh-manager.conf
done
dialog --backtitle "TUI SSH Manager" --title "Deleting server" --msgbox "Server deleted successfully." 6 35
else
if [[ $dans = [Nn] || $dans = [Nn][Oo] ]]; then
dialog --backtitle "TUI SSH Manager" --title "Deleting server" --msgbox "Canceling server deletion." 6 35
else
dialog --backtitle "TUI SSH Manager" --title "Deleting server" --msgbox "Server has not been removed." 6 35
fi
fi
fi
}
# Connect to server via SSH with X11
x11_ssh() {
connectbox=""
isTrusted=""
scounter=0
stcounter=0
maxname_len=0
for server in "${!SSH_CONFIGS[@]}"; do
if [ $maxname_len -lt ${#server} ]; then maxname_len=${#server}; fi
done
for server in "${!SSH_CONFIGS[@]}"; do
connectbox="$connectbox$server"
COUNTER=$((maxname_len - ${#server}))
until [ $COUNTER -eq 0 ]; do
connectbox=$connectbox" "
let COUNTER-=1
done
connectbox=$connectbox" --- ($scounter)\n"
scounter=$((scounter + 1))
done
server_name=$(dialog --backtitle "TUI SSH Manager" --title "Connect to server via SSH w/ X11" --inputbox "Available server(s):\n$connectbox\n\nSelect server:" 18 55 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then return 0; fi
if [ -z $server_name ]; then dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Field cannot be empty!" 6 35 && return 1; fi
# If the input is a number
if [[ "$server_name" =~ ^[0-9]+$ ]]; then
for server in "${!SSH_CONFIGS[@]}"; do
if [ $stcounter -eq $server_name ]; then server_name=$server && break; fi
stcounter=$((stcounter + 1))
done
fi
if [[ ! ${SSH_CONFIGS[$server_name]+_} ]]; then
dialog --backtitle "TUI SSH Manager" --title "ERROR" --msgbox "Server Not found." 6 35
else
temp_var=${SSH_CONFIGS[$server_name]}
cutservername=$(echo $temp_var | cut -d' ' -f 1)
connectbox="Connecting to $cutservername"
if [[ $temp_var = *"-p"* ]]; then
serverport=$(echo "${temp_var/-p/"&"}" | cut -d'&' -f 2);
connectbox=$connectbox" on port: $serverport"
fi
connectbox=$connectbox"...\n"
connectbox=$connectbox"Are you sure you want to connect?"
dialog --backtitle "TUI SSH Manager" --title "Connecting..." --yesno "$connectbox" 8 55
confirm=$?
if [[ $confirm -eq 0 ]]; then
dialog --backtitle "TUI SSH Manager" --title "X11" --yes-label "Trusted (Y)" --no-label "Untrusted (X)" --defaultno --yesno "What X11 mode do you want to use for connection?" 8 55
if [ $? -eq 0 ]; then isTrusted="Y"; else isTrusted="X"; fi
printf '\033[2J\033[3J\033[1;1H'
ssh -"$isTrusted" ${SSH_CONFIGS[$server_name]}
fi
fi
}
# Menu
while true; do
MENUVAR=$(dialog --backtitle "TUI SSH Manager" --title "MENU" --menu "Select an option:" 8 35 0 1 "Setting SSH configuration" \
2 "Show SSH configuration" 3 "Connect to SSH" 4 "Delete server" 5 "Send data via SCP" 6 "Connect to SSH [X11]" 7 "Exit" 3>&1 1>&2 2>&3 3>&-)
if [ $? -eq 1 ]; then MENUVAR=7; fi ## Cancel will exit
printf '\033[2J\033[3J\033[1;1H'
case $MENUVAR in
1) configure_ssh;; #OK
2) show_config;; #OK
3) connect_ssh;; #OK
4) delete_server;; #OK
5) fileshare_scp;; #OK
6) x11_ssh;; #OK
7) printf "$(tput bold)$(tput setaf 2)Closing SSH Manager...$(tput sgr0)\n" && break;;
esac
done