-
Notifications
You must be signed in to change notification settings - Fork 7
/
tunnel.sh
executable file
·35 lines (30 loc) · 909 Bytes
/
tunnel.sh
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
#!/bin/bash
tunnel_chk() {
# Your Individual SSH Connection Info.
# PEM_LOC is optional parameter for SSH login.
PEM_LOC="/path/to/your/pem/login.pem"
ID="yourID"
ADDR="home.address.com"
PORT="22"
PEMSTR=""
if [ -n "$PEM_LOC" ]; then
PEMSTR="-i $PEM_LOC"
fi
if [ $1 = "L" ]; then
foo="$(echo > /dev/tcp/localhost/$2)"
if [ $? != 0 ]; then # port is close
bar="$(pkill -f $2:$3:$4)"
ssh -fN $PEMSTR -L $2:$3:$4 $ID@$ADDR -p $PORT
fi
else
foo="$(echo > /dev/tcp/$ADDR/$2)"
if [ $? != 0 ]; then # port is close
bar="$(pkill -f $2:$3:$4)"
ssh -fN $PEMSTR -R $2:$3:$4 $ID@$ADDR -p $PORT
fi
fi
}
# Usage
# tunnel_chk L/R port:host:port timeout_interval
# You can add multiple lines of this functions: SSH tunneling connections.
tunnel_chk R 2222 localhost 22 5