AutoDevReverseProxy/script/myssh

130 lines
3 KiB
Text
Raw Normal View History

2022-11-11 22:29:26 +01:00
#!/usr/bin/env bash
if [ -z "$SQL_CLI_TEMPLATE" ]
then
if [ $(uname -s) = "Linux" ]
then
2022-12-21 19:30:38 +01:00
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
else
2022-12-21 19:30:38 +01:00
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:6033\" -a \"Sequel Ace\"'
fi
else
echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE"
read -r -p "Continue [Y/n] " CONTINUE
if [[ ! $CONTINUE =~ "^[Yy]" ]]
then
exit
fi
fi
2022-11-11 22:29:26 +01:00
[ -z $SQL_PROXY_HOST ] && SQL_PROXY_HOST="localhost"
2022-11-17 13:01:07 +01:00
CONNECTION_CACHE="$HOME/.cache/sqlproxy_$SQL_PROXY_HOST"
2022-11-11 22:29:26 +01:00
2022-11-18 11:56:29 +01:00
HELP="Usage: myssh [ls|connect]\n
SUBCOMMANDS:\n
ls: list available database hosts\n
connect: connect to a database host\n
disconnect: Quit existing ssh master session\n
\n
2022-11-11 22:29:26 +01:00
SYNTAX connect host [-u user] [-p password] [-c client]
"
2022-11-17 13:01:07 +01:00
ssh_status() {
ssh -O check -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST > /dev/null 2>&1
echo $?
2022-11-11 22:29:26 +01:00
}
2022-11-17 13:01:07 +01:00
connect() {
mkdir -p $HOME/.ssh/controlmasters
if [ $(ssh_status) -ne 0 ]
then
echo "" > $CONNECTION_CACHE
ssh -o "ControlPersist=10m" -M -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
fi
}
disconnect() {
if [ $(ssh_status) -eq 0 ]
then
ssh -O stop -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
fi
}
port_forward() {
ACTIVE_HOST=$(cat $CONNECTION_CACHE)
2022-12-21 19:30:38 +01:00
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$2 ]
2022-11-11 22:29:26 +01:00
then
2022-11-17 13:01:07 +01:00
if [ ! -z $ACTIVE_HOST ]
2022-11-11 22:29:26 +01:00
then
2022-12-21 19:30:38 +01:00
ssh -O cancel -L 6033:$ACTIVE_HOST -S $HOME/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST} q
2022-11-11 22:29:26 +01:00
fi
2022-12-21 19:30:38 +01:00
echo $ACTIVE_HOST
echo $1 $2
ssh -O forward -L 6033:$1:$2 -S ${HOME}/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST}
2022-11-11 22:29:26 +01:00
fi
2022-12-21 19:30:38 +01:00
echo $2:$3 > "${CONNECTION_CACHE}"
2022-11-11 22:29:26 +01:00
}
2022-11-17 13:01:07 +01:00
ls_hosts() {
2022-12-21 19:30:38 +01:00
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" ls
}
get_host() {
if [ $1 == '' ]
then
printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n'
else
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1"
fi
}
set_host_env() {
if [ $1 == 'mysql' | $1 == 'psql' ]
then
TARGET_HOST_TYPE=$1
fi
if [ $2 != '' ]
then
fi
2022-11-11 22:29:26 +01:00
}
run_client() {
$(eval echo $SQL_CLI_TEMPLATE)
2022-11-11 22:29:26 +01:00
}
MAIN_OPTION=$1
shift
# ensure connection
connect
2022-11-11 22:29:26 +01:00
case $MAIN_OPTION in
ls)
2022-11-17 13:01:07 +01:00
ls_hosts;;
2022-11-11 22:29:26 +01:00
connect)
# check if host is valid
TARGET_HOST=$1
shift
2022-12-21 19:30:38 +01:00
while getopts "u:p:" o
do
case "$o" in
u) MYSQL_USERNAME="$OPTARG" ;;
p) MYSQL_PASSWORD="$OPTARG" ;;
esac
done
TARGET_HOST_DATA="$(get_host "${TARGET_HOST}")"
port_forward $TARGET_HOST_DATA
# port_forward $TARGET_HOST
# if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ]
# then
# run_client
# fi
2022-11-11 22:29:26 +01:00
;;
2022-11-17 13:01:07 +01:00
disconnect)
disconnect;;
2022-11-11 22:29:26 +01:00
*)
2022-11-18 11:56:29 +01:00
echo -e $HELP;;
2022-11-11 22:29:26 +01:00
esac