#!/bin/bash # connect to iot services THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/../tools/common.sh" # get target settings # provides: SERVER="" PORT="" SSH_USERNAME="" SSH_OPTIONS="" get_server_meta() { local domain=${1##*.} local host=${1%.*} if [[ "$host" == "$domain" ]]; then domain="" fi case $domain in ibd|ebd ) SERVER=$host.$domain.ink PORT=12022 SSH_USERNAME=root ;; nasp ) SERVER=$host PORT=22 SSH_USERNAME=dictxiong SSH_OPTIONS='-o ProxyJump="ssh@nasp.ob.ac.cn:36022"' ;; "" ) SERVER=proxy.beardic.cn local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]") tmp=${tmp:0:4} PORT=$((tmp+36000)) SSH_USERNAME=root ;; * ) fmt_fatal "unknown domain: $domain" esac } # ssh run_ssh() { 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), sshl (ssh -L)" } router() { if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then print_help exit fi get_server_meta "$1" case $2 in -h|--help) print_help exit ;; ssh|"" ) run_ssh ;; sshl ) run_sshl "$3" ;; * ) print_help fmt_fatal "unknown command: $2" ;; esac } router "${GOT_OPTS[@]}"