dotwryn/zsh/ssh

95 lines
2.6 KiB
Plaintext
Raw Normal View History

2021-02-23 20:38:09 +00:00
#!/bin/zsh
alias c='WRYN_REMOTE_CONNECTOR'
2021-02-26 21:30:07 +00:00
alias cmux='WRYN_REMOTE_CONNECTOR tmux'
2021-02-23 20:38:09 +00:00
alias sshc='WRYN_REMOTE_CONNECTOR ssh'
alias sshe='$EDITOR $WRYN_SSH_CONNECTION_LIST'
alias ssh-add-all='WRYN_SSH_ADD_CREDENTIALS'
WRYN_SSH_CONNECTION_LIST="$HOME/.config/wryn/ssh-connections"
2021-02-24 01:35:19 +00:00
WRYN_SSH_DELIMITER=' *| '
2021-02-23 20:38:09 +00:00
[ -d "$HOME/.config/wryn" ] && [ ! -f "$WRYN_SSH_CONNECTION_LIST" ] && touch "$WRYN_SSH_CONNECTION_LIST";
2021-02-23 22:15:56 +00:00
WRYN_SSH_ADD_CREDENTIALS() {
2021-02-24 01:35:19 +00:00
for key in $(grep -slR "PRIVATE" "$HOME/.ssh"); do
2021-02-25 16:33:50 +00:00
ssh-add "$key";
2021-02-23 22:15:56 +00:00
done
}
2021-02-23 20:38:09 +00:00
function WRYN_REMOTE_CONNECTOR() {
2021-02-23 22:15:56 +00:00
local COMMAND="$1";
local CONNECTION="$2";
2021-02-23 20:38:09 +00:00
2021-02-25 18:46:59 +00:00
local SSH_ID="^$CONNECTION$WRYN_SSH_DELIMITER"
2021-02-23 20:38:09 +00:00
[ $2 ] \
|| { echo 'Must specify a connection'; return 1 };
grep -q "$SSH_ID" "$WRYN_SSH_CONNECTION_LIST" >/dev/null 2>&1 \
|| { echo 'No such connection'; return 2 };
case $COMMAND in;
2021-03-03 20:33:18 +00:00
sshfs ) sshfs $(WRYN_SSH_PARSE_ARGS $SSH_ID):$3 ${@:4} ;;
ssh ) ssh $(WRYN_SSH_PARSE_ARGS $SSH_ID) ${@:3} ;;
cp-to ) scp ${@:5} $(WRYN_SCP_PARSE_ARGS $SSH_ID $3):$4 ;;
cp-from ) scp ${@:5} $(WRYN_SCP_PARSE_ARGS $SSH_ID):$3 $4 ;;
copy-id ) ssh-copy-id ${@:3} $(WRYN_SSH_PARSE_ARGS $SSH_ID) ;;
tmux ) WRYN_REMOTE_TMUX "$SSH_ID" ${@:3} ;;
2021-02-24 01:35:19 +00:00
2021-02-23 20:38:09 +00:00
* ) echo "$COMMAND not implemented" ;;
esac
}
_WRYN_REMOTE_CONNECTOR() {
local state;
_arguments \
'1: :->remote_command'\
'2: :->connections'\
':: :->remote_args'\
;
case "$state" in
remote_command)
2021-02-26 21:30:07 +00:00
compadd -X 'Choose a connection command:' \
ssh sshfs cp-to cp-from copy-id tmux
2021-02-23 20:38:09 +00:00
;;
connections)
2021-02-25 21:02:48 +00:00
compadd -X 'Choose a connection:' $(sed -n "s/$WRYN_SSH_DELIMITER.*//p" "$WRYN_SSH_CONNECTION_LIST")
2021-02-23 20:38:09 +00:00
;;
remote_args)
_files
case $words[2] in;
ssh ) _ssh;;
sshfs ) _sshfs;;
2021-02-24 01:35:19 +00:00
cpto | cpfrom ) _scp;;
2021-02-26 21:30:07 +00:00
tmux ) _tmux;;
2021-02-23 20:38:09 +00:00
* ) ;;
esac
;;
esac
}
compdef _WRYN_REMOTE_CONNECTOR WRYN_REMOTE_CONNECTOR;
2021-02-23 22:15:56 +00:00
WRYN_SSH_PARSE_ARGS() {
SSH_ID="$1";
sed -n "s/$SSH_ID\(.*\)$/\1/p" "$WRYN_SSH_CONNECTION_LIST"
2021-02-23 20:38:09 +00:00
}
2021-02-24 01:35:19 +00:00
WRYN_SCP_PARSE_ARGS() {
SSH_ID="$1";
[ $2 ]\
&& sed -n "s>$SSH_ID *-p \([^ ]*\)\(.*\)$>-P \1 $2\2>p" "$WRYN_SSH_CONNECTION_LIST"\
|| sed -n "s/$SSH_ID *-p\(.*\)$/-P\1/p" "$WRYN_SSH_CONNECTION_LIST"
}
2021-03-03 20:33:18 +00:00
WRYN_REMOTE_TMUX() {
local SSH_ID="$1"
local TMUX_UTF8_FLAG=$(locale charmap | grep -q 'UTF-8' && echo '-u')
local DEFAULT_TMUX_SESSION_NAME='main'
[ ! $2 ] \
&& ssh $(WRYN_SSH_PARSE_ARGS $SSH_ID) -t tmux new-session -A -s $DEFAULT_TMUX_SESSION_NAME;
[ $3 ] \
&& ssh $(WRYN_SSH_PARSE_ARGS $SSH_ID) -t tmux $TMUX_UTF8_FLAG ${@:2} \
|| ssh $(WRYN_SSH_PARSE_ARGS $SSH_ID) -t tmux $TMUX_UTF8_FLAG a -t $2;
}