2022-11-11 22:29:26 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
[ -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
|
|
|
|
|
|
|
HELP="Usage: myssh [ls|connect]
|
|
|
|
SUBCOMMANDS:
|
|
|
|
ls: list available database hosts
|
|
|
|
connect: connect to a database host
|
2022-11-17 13:01:07 +01:00
|
|
|
disconnect: Quit existing ssh master session
|
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)
|
|
|
|
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1 ]
|
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-11-17 13:01:07 +01:00
|
|
|
ssh -O cancel -L 3306:$ACTIVE_HOST:3306 -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
|
2022-11-11 22:29:26 +01:00
|
|
|
fi
|
2022-11-17 13:01:07 +01:00
|
|
|
ssh -O forward -L 3306:$1:3306 -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST
|
2022-11-11 22:29:26 +01:00
|
|
|
fi
|
2022-11-17 13:01:07 +01:00
|
|
|
echo $1 > $CONNECTION_CACHE
|
2022-11-11 22:29:26 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 13:01:07 +01:00
|
|
|
ls_hosts() {
|
|
|
|
echo $(ssh -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST ls)
|
2022-11-11 22:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
run_client() {
|
|
|
|
if [ $(uname -s) = "Linux" ]
|
|
|
|
then
|
|
|
|
mysql --protocol=TCP -u $1 -p$2 -h localhost -P 3306
|
|
|
|
else
|
|
|
|
open "mysql://$1:$2@localhost:3306" -a "Sequel Ace"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
MAIN_OPTION=$1
|
|
|
|
shift
|
|
|
|
|
2022-11-17 15:26:23 +01:00
|
|
|
# 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-11-17 13:01:07 +01:00
|
|
|
ls_hosts | grep -qe "^$TARGET_HOST$"
|
2022-11-11 22:29:26 +01:00
|
|
|
GREP_EXIT_CODE=$?
|
|
|
|
if [ $GREP_EXIT_CODE -eq 0 ]
|
|
|
|
then
|
|
|
|
while getopts "u:p:" o
|
|
|
|
do
|
|
|
|
case "$o" in
|
|
|
|
u) MYSQL_USERNAME="$OPTARG" ;;
|
|
|
|
p) MYSQL_PASSWORD="$OPTARG" ;;
|
|
|
|
esac
|
|
|
|
done
|
2022-11-17 13:01:07 +01:00
|
|
|
port_forward $TARGET_HOST
|
2022-11-11 22:29:26 +01:00
|
|
|
if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ]
|
|
|
|
then
|
|
|
|
run_client $MYSQL_USERNAME $MYSQL_PASSWORD
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Invalid Hostname: $2."
|
|
|
|
fi
|
|
|
|
;;
|
2022-11-17 13:01:07 +01:00
|
|
|
disconnect)
|
|
|
|
disconnect;;
|
2022-11-11 22:29:26 +01:00
|
|
|
*)
|
|
|
|
echo -e "Usage: myssh [ls|connect]\n\n";;
|
|
|
|
esac
|