Files
dotfiles/dot_local/bin/ssh-pick
T

53 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# ssh-pick — выбрать SSH-хост через fzf
set -euo pipefail
SSH_CONFIG_DIR="${HOME}/.ssh/config.d"
SSH_CONFIG="${HOME}/.ssh/config"
_list_hosts() {
local configs=()
if [[ -d "$SSH_CONFIG_DIR" ]]; then
configs=("$SSH_CONFIG_DIR"/*.conf)
fi
[[ -f "$SSH_CONFIG" ]] && configs+=("$SSH_CONFIG")
for f in "${configs[@]}"; do
[[ -f "$f" ]] || continue
awk '/^Host [^*]/{
sub(/^Host /, "")
n = split($0, hosts, " ")
for (i = 1; i <= n; i++) print hosts[i]
}' "$f"
done | sort -u
}
main() {
if ! command -v fzf &>/dev/null; then
echo "fzf not found"
exit 1
fi
local hosts
hosts=$(_list_hosts)
if [[ -z "$hosts" ]]; then
echo "No SSH hosts found"
exit 1
fi
local selected
selected=$(echo "$hosts" | fzf \
--prompt "ssh > " \
--header "Enter — подключиться | Ctrl-C — выйти" \
--height=40% \
--reverse) || exit 0
echo "▶ ssh $selected"
exec ssh "$selected"
}
main "$@"