Fish · 1037 bytes Raw Blame History
1 ## pick a host from ~/.ssh/config and tailscale peers
2 #
3 function sshf
4 set -l cfghosts (awk '/^Host[[:space:]]+/ {for (i=2;i<=NF;i++) if ($i !~ /[*?]/) print $i}' ~/.ssh/config 2>/dev/null)
5 for f in ~/.ssh/config.d/*.conf
6 test -r $f; and set cfghosts $cfghosts (awk '/^Host[[:space:]]+/ {for (i=2;i<=NF;i++) if ($i !~ /[*?]/) print $i}' $f)
7 end
8 set -l lines
9 for h in $cfghosts; set lines $lines $h; end
10 if type -q tailscale; and type -q jq
11 set -l ts (tailscale status --json | jq -r '.Peer[] | "\(.HostName)\t\(.TailscaleIPs[0])"')
12 set lines $lines $ts
13 end
14 test (count $lines) -gt 0; or begin; echo "no hosts found"; return 1; end
15 set -l sel (printf '%s\n' $lines | column -t -s \t | fzf --prompt="ssh> " --preview 'echo {}')
16 test -n "$sel"; or return
17 set -l last (echo $sel | awk '{print $NF}')
18 if string match -qr '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' -- $last
19 set host $last
20 else
21 set host (echo $sel | awk '{print $1}')
22 end
23 ssh $host
24 end