-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshko.sh
More file actions
executable file
·153 lines (137 loc) · 4.54 KB
/
sshko.sh
File metadata and controls
executable file
·153 lines (137 loc) · 4.54 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
#!/bin/bash
logFile=~/.sshLog.txt
[[ -e "$logFile" ]] || : > "$logFile"
# -------- Formatting helpers --------
fmt_row() {
local left="$1" note="$2" left_w="$3" note_w="$4"
note="${note//$'\n'/ }"
[[ ${#left} -gt $left_w ]] && left="${left:0:$((left_w-1))}…"
[[ ${#note} -gt $note_w ]] && note="${note:0:$((note_w-1))}…"
printf "%-${left_w}s | %-${note_w}s" "$left" "$note"
}
# -------- File helpers --------
# Build arrays from file:
# menu_pairs[]: <idx> "<formatted row>"
# specs[]: left field (ssh spec)
# notes[]: note
# lines[]: trimmed full line as stored
build_arrays() {
menu_pairs=(); specs=(); notes=(); lines=()
local key=0
while IFS= read -r raw || [[ -n "$raw" ]]; do
[[ -z "$raw" || "$raw" =~ ^[[:space:]]*# ]] && continue
# Normalize line (trim)
local line="$raw"
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
local left="${line%%,*}"
local note="${line#*,}"
[[ "$note" == "$line" ]] && note=""
# Trim fields
left="${left#"${left%%[![:space:]]*}"}"; left="${left%"${left##*[![:space:]]}"}"
note="${note#"${note%%[![:space:]]*}"}"; note="${note%"${note##*[![:space:]]}"}"
local label; label=$(fmt_row "$left" "$note" "$LEFT_W" "$NOTE_W")
menu_pairs+=("$key" "$label")
specs+=("$left")
notes+=("$note")
lines+=("$line")
((key++))
done < "$logFile"
}
# Delete entry by index safely (rewrite file)
delete_index() {
local del="$1"
local tmp
tmp=$(mktemp)
local i=0
while IFS= read -r raw || [[ -n "$raw" ]]; do
# keep comments/blank lines as-is in output
if [[ -z "$raw" || "$raw" =~ ^[[:space:]]*# ]]; then
echo "$raw" >> "$tmp"
continue
fi
# Align with lines[] by trimming same way
local line="$raw"
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
# Only count non-comment, non-empty lines toward index
if [[ $i -ne $del ]]; then
echo "$line" >> "$tmp"
fi
((i++))
done < "$logFile"
mv "$tmp" "$logFile"
}
# -------- Menu / actions --------
show_menu() {
while true; do
if ! [ -s "$logFile" ]; then
echo "ssh log file is empty"
exit 1
fi
HEIGHT=22
WIDTH=90
CHOICE_HEIGHT=18
BACKTITLE="This is made, because you are super lazy!!!!"
TITLE="SSHko"
MENU="Choose one of the following ssh connections:"
LEFT_W=48
NOTE_W=$(( WIDTH - 10 - LEFT_W ))
build_arrays
if [[ ${#menu_pairs[@]} -eq 0 ]]; then
echo "ssh log file is empty"
exit 1
fi
header=$(fmt_row "SSH" "NOTE" "$LEFT_W" "$NOTE_W")
CHOICE=$(dialog --clear --title "$TITLE" --backtitle "$BACKTITLE" \
--ok-label "Connect" --cancel-label "Exit" \
--extra-button --extra-label "Delete" \
--menu "$MENU\n$header" \
$HEIGHT $WIDTH $CHOICE_HEIGHT "${menu_pairs[@]}" \
2>&1 >/dev/tty)
status=$?
clear
case $status in
0) # Connect
[[ -z "$CHOICE" ]] && continue
spec="${specs[$CHOICE]}"
IFS=' ' read -r -a SSHARGS <<< "$spec"
exec ssh "${SSHARGS[@]}"
;;
3) # Delete (extra button)
[[ -z "$CHOICE" ]] && continue
full_line="${lines[$CHOICE]}"
dialog --yesno "Delete this entry?\n\n${full_line}" 8 70
confirm=$?
clear
if [[ $confirm -eq 0 ]]; then
delete_index "$CHOICE"
# Loop to refresh menu after deletion
fi
;;
1|255) # Cancel / ESC
exit 0
;;
esac
done
}
# -------- Main behavior --------
if [[ $# -eq 0 ]] || [[ $# -gt 1 && "$1" == "--menu" ]]; then
show_menu
exit
fi
# Called with args: treat ALL args as ssh spec; add if missing, then connect
sshSpec="$*"
# Exists already?
if awk -v s="$sshSpec" -F',' '
{ gsub(/^[ \t]+|[ \t]+$/,"",$1); if ($1==s){found=1; exit} }
END { exit found?0:1 }
' "$logFile"; then
IFS=' ' read -r -a SSHARGS <<< "$sshSpec"
exec ssh "${SSHARGS[@]}"
else
read -r -p "Enter a note for [${sshSpec}] (optional): " note
echo "${sshSpec}, ${note}" >> "$logFile"
IFS=' ' read -r -a SSHARGS <<< "$sshSpec"
exec ssh "${SSHARGS[@]}"
fi