From 45f4d9538c3ff35f242f3415dd68f4408dab0f66 Mon Sep 17 00:00:00 2001 From: "xiongdian.me" Date: Tue, 28 Feb 2023 19:47:17 +0800 Subject: [PATCH] riot: introduce sshl --- scripts/riot | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/scripts/riot b/scripts/riot index 09f2efe..e81c3b1 100755 --- a/scripts/riot +++ b/scripts/riot @@ -43,21 +43,39 @@ get_server_meta() # ssh run_ssh() { - fmt_note "--> ssh to $SERVER:$PORT" - eval ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER + CMD="ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER" + fmt_note "-->" $CMD + eval $CMD +} + +# sshl +run_sshl() +{ + if [[ -z "$1" || "$1" != *":"* ]]; then + fmt_fatal "invalid remote address: $1" + fi + while + local port=$(shuf -n 1 -i 49152-65535) + netstat -atun | grep -q "$port" + do + continue + done + CMD="ssh -p $PORT $SSH_OPTIONS -NC -L $port:$1 $SSH_USERNAME@$SERVER" + fmt_note "-->" $CMD + fmt_note " > please access localhost:$port" + eval $CMD } # main print_help() { fmt_info "usage: $0 [command] [options]" - echo "available commands: ssh (default)" + echo "available commands: ssh (default), sshl (ssh -L)" } router() { - test -n "$1" || (print_help && fmt_fatal "invalid arguments") - if [[ "$1" == "-h" || "$1" == "--help" ]]; then + if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then print_help exit fi @@ -70,6 +88,9 @@ router() ssh|"" ) run_ssh ;; + sshl ) + run_sshl "$3" + ;; * ) print_help fmt_fatal "unknown command: $2"