Last active
January 5, 2016 15:58
-
-
Save index0h/9532878 to your computer and use it in GitHub Desktop.
mysql-tunnel.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Необходимо задать три массива: PORTS, PROXY_HOSTS и HOSTS. | |
# При подключении будут созданы туннели PROXY_HOSTS[id]:PORTS[id] -> HOSTS[id]:3306. | |
# HOSTS - массив хостов, к которым будет выполнятся ssh подключение (рекомендую использовать ssh-ключи). | |
PORTS=('33061') | |
PROXY_HOSTS=('127.0.0.1') | |
HOSTS=('external.host') | |
case "$1" in | |
'start') | |
echo 'Включение MySQL туннелирования.' | |
for ((i = 0; i < ${#PORTS[@]}; i++)) do | |
ssh -4 -f -N -L ${PORTS[$i]}:${PROXY_HOSTS[$i]}:3306 ${HOSTS[$i]} | |
echo ${HOSTS[$i]} - ${PORTS[$i]} | |
done | |
;; | |
'stop') | |
echo 'Выключение MySQL туннелирования.' | |
for ((i = 0; i < ${#PORTS[@]}; i++)) do | |
COMMAND="[s]sh -4 -f -N -L ${PORTS[$i]}:${PROXY_HOSTS[$i]}:3306 ${HOSTS[$i]}" | |
PID=`ps ax | grep "$COMMAND" | awk '{ print $1 }'` | |
kill $PID | |
echo ${HOSTS[$i]} - ${PORTS[$i]} | |
done | |
;; | |
'restart') | |
$0 stop | |
$0 start | |
;; | |
*) | |
echo "Использование: $SELF {start|stop|restart}" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment