-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf_ssh.sh
executable file
·67 lines (58 loc) · 1.6 KB
/
fzf_ssh.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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/fzf/fzf_ssh.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/fzf
# date: 2024-06-20T17:17:17+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
ssh_config="$HOME/.ssh/config"
edit="$EDITOR"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to open configured ssh sessions
Usage:
$script
Examples:
$script"
[ -n "$1" ] \
&& printf "%s\n" "$help" \
&& exit 0
while true; do
# menu
select=$(printf "%s\n" \
"$(grep "^Host " "$ssh_config" \
| cut -d ' ' -f2 \
)" \
"edit config" \
| fzf -m --cycle \
--bind 'focus:transform-preview-label:echo [ {} ]' \
--preview-window "right:75%" \
--preview "case {} in
\"edit config\")
cat \"$ssh_config\"
;;
*)
sed -n '/^Host {}$/,/^$/p' \"$ssh_config\"
;;
esac" \
)
# select executable
case "$select" in
"edit config")
"$edit" "$ssh_config"
;;
*)
session=$(printf "%s" "$select" | wc -w)
for host in $select; do
session=$((session-1))
[ $session -ge 0 ] && [ -n "$DISPLAY" ] \
&& $TERMINAL -T "ssh $host" -e ssh "$host"
[ $session -eq 0 ] && [ -z "$DISPLAY" ] \
&& ssh "$host"
done
break
;;
esac
done