dotfiles/scripts/riot

81 lines
1.6 KiB
Bash
Executable File

#!/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()
{
fmt_note "--> ssh to $SERVER:$PORT"
eval ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER
}
# main
print_help()
{
fmt_info "usage: $0 <service> [command] [options]"
echo "available commands: ssh (default)"
}
router()
{
test -n "$1" || (print_help && fmt_fatal "invalid arguments")
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
print_help
exit
fi
get_server_meta "$1"
case $2 in
-h|--help)
print_help
exit
;;
ssh|"" )
run_ssh
;;
* )
print_help
fmt_fatal "unknown command: $2"
;;
esac
}
router "${GOT_OPTS[@]}"