-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtunnel
More file actions
executable file
·58 lines (50 loc) · 1.35 KB
/
tunnel
File metadata and controls
executable file
·58 lines (50 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# mysql tunnel to remote server via jumphost
# ports on the jumphost servers
Ports[1]=10008
Ports[2]=10009
Ports[3]=10010
Ports[4]=10011
Ports[5]=10012
Ports[6]=10013
Ports[7]=10014
# jumphost server
JUMPHOST=''
count=${#Ports[@]}
if [[ -z $1 || $1 -gt $count || $1 -lt 1 ]]; then
echo -e 'Usage:\ntunnel VipNo [LocalPort]'
echo 'default LocalPort = 3306'
echo -e '\nJumphosts ports:'
echo -e "\nVipNo\tPort\n"
for (( i = 1 ; i <= $count ; i++ ))
do
echo -e $i "\t("${Ports[$i]}")"
done
else
if [ -z $2 ]; then
LocalPort=3306
else
LocalPort=$2
fi
portCheck="ssh -fNg -L $LocalPort:127.0.0.1:"
oldPID=`ps ax | grep "$portCheck" | grep -iv "grep" | cut -d"?" -f1`
if [ -z $oldPID ]; then
echo 'No previous process found, setting tunnel...'
else
echo "Found previous tunnel bound to port $LocalPort (pid $oldPID), refreshing tunnel..."
kill -15 $oldPID
if [ "$?" = "0" ]; then
echo "Process killed successfully"
else
echo "Cannot kill process $oldPID, you need to kill it manually"
exit 1
fi
fi
ssh -fNg -L $LocalPort:127.0.0.1:${Ports[$1]} $JUMPHOST
if [ "$?" = "0" ]; then
echo "Tunnel ready"
else
echo "Tunnel cannot be created"
exit 1
fi
fi