2024-11-08 13:27:12 +03:00

36 lines
1018 B
Bash
Executable File

#!/bin/sh
set -eu
export FZF_DEFAULT_OPTS="\
${FZF_DEFAULT_OPTS}
--border-label=' Start SSH session '
--preview-window=hidden
--preview=''
"
ssh_hosts="$(grep 'Host ' "${HOME}/.ssh/config" | sed 's/^Host //')"
etc_hosts="$(grep -v '^#' /etc/hosts)"
history_hosts="$(grep ssh "${HOME}/.zsh_history" | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
all_hosts="$(echo "${ssh_hosts}" "${etc_hosts}" "${history_hosts}" | grep -v '\*'| sed 's/ /\n/g ; /^$/d' | sort | uniq | tac)"
choosed_ssh_server="$(echo "${all_hosts}" | fzf | tail -n 1)"
if [ -z "${choosed_ssh_server}" ]; then
exit 0
fi
SESSION="ssh_$(echo "${choosed_ssh_server%% *}" | sed 's/\./\-/g ; s/\:/\-/g')"
if ! tmux has-session -t "${SESSION}" 2>/dev/null; then
tmux new-session -s "${SESSION}" -d -n "1" "TERM=xterm ssh ${choosed_ssh_server} || read"
fi
tmux set-option -t "${SESSION}" status off
tmux set-option -t "${SESSION}" prefix None
tmux set-option -t "${SESSION}" key-table off
tmux switch-client -t "${SESSION}"