From dbb41dd29e1d4cd00cc104bdfc56ede989470b76 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 18 Apr 2022 20:19:48 +0200 Subject: [PATCH] Add forward-local-port --- .config/zsh/completions/_forward-local-port | 11 ++++++++++ .config/zsh/functions/forward-local-port | 23 +++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 .config/zsh/completions/_forward-local-port create mode 100755 .config/zsh/functions/forward-local-port diff --git a/.config/zsh/completions/_forward-local-port b/.config/zsh/completions/_forward-local-port new file mode 100644 index 0000000..d1bcf51 --- /dev/null +++ b/.config/zsh/completions/_forward-local-port @@ -0,0 +1,11 @@ +#compdef forward-local-port + +_arguments '-h[short help text]' \ + '--help[short help text]' \ + '--local-port[local port to forward]:port:' \ + '--remote-port[remote port to listen on]:port:' \ + '1:host:_ssh_hosts' + +# Local Variables: +# mode: shell-script +# End: diff --git a/.config/zsh/functions/forward-local-port b/.config/zsh/functions/forward-local-port new file mode 100755 index 0000000..abb433b --- /dev/null +++ b/.config/zsh/functions/forward-local-port @@ -0,0 +1,23 @@ +#!/usr/bin/env zsh +# Forward a local port to a server via SSH tunnel. + +setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL + +zmodload zsh/zutil +local -a o_help=() +local -a o_lport=() +local -a o_rport=() +zparseopts -D -K -- h=o_help -help=o_help -local-port:=o_lport -remote-port:=o_rport +if [[ ${#o_help} -ne 0 || ! -v 1 || ${#o_lport} -ne 2 || ${#o_rport} -ne 2 ]]; then + local ret=$(( ${#o_help} ^ 1 )) + print -u $(( 1 + ${ret} )) "usage: ${0} [-h|--help]" \ + "--local-port --remote-port " + return ${ret} +fi +local host=${1} +local lport=${o_lport[2]} +local rport=${o_rport[2]} + +print -P "%F{12}Forwarding local port%f %F{13}${lport}%f %F{12}over SSH tunnel" \ + "to%f" "%F{13}${host}%f %F{12}port%f %F{13}${rport}%f %F{12}…%f" +ssh -R ${rport}:"[::1]":${lport} -N -T ${host}