From 97debc8da1e21165eb8c4426964a10b4c69a5138 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 2 Aug 2024 11:21:19 -0500 Subject: [PATCH] rewrite tunnel in nushell --- home/private_bin/executable_tunnel | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 home/private_bin/executable_tunnel diff --git a/home/private_bin/executable_tunnel b/home/private_bin/executable_tunnel new file mode 100644 index 0000000..44d2a63 --- /dev/null +++ b/home/private_bin/executable_tunnel @@ -0,0 +1,49 @@ +#!/usr/bin/env nu +# TODO: add tunnel show subcmd to quickly view open tunnels + +def start-ssh-controller [host: string] { + print "initializing connection to $host" + ssh -M -f -N $host +} + +def is-null [ name: string, arg: any] { + if $arg == null { + print $"expected argument for --($name)" + exit 1 + } +} + +# script `--help` will improve :) +# https://github.com/nushell/nushell/pull/13445 + +# control background tunnels +# usage: +# tunnel up -p 4321 -h computer +# tunnel down -h computer +def main [] { + print "use subcommands up/down to control ssh tunnels" + help main +} + +# activate ssh tunnel +def "main up" [ + --port: int # port number + --host: string # host name +] { + is-null "port" $port + is-null "host" $host + + if (ssh -O check $host | complete).exit_code != 0 { + start-ssh-controller $host + } + ssh -fNL $"($port):localhost:($port)" $host +} + +# shutdown ssh tunnels on host +def "main down" [ + --host: string # host name +] { + is-null "host" $host + print "exiting connection to $host" + ssh -O exit $host +}