-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_tunnel.sh
executable file
·108 lines (91 loc) · 1.82 KB
/
ssh_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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
usage() {
cat << EOF
usage: `basename $0` [options]
This script establishes an ssh tunnel through the hostname provided.
OPTIONS:
-h Show this message
-r Hostname of server through which you wish to ssh tunnel
-l Hostname login or username
-d Local device you wish to activate SOCKS proxy for.
-p Port through which you wish to tunnel (default: 8080)
-x Disable ssh tunnel and proxy configuration
EOF
}
USERNAME=
HOSTNAME=
DEVICE=
DISABLE=
PORT=8080
ISMAC=0
if [ $(uname)=="Darwin" ]; then
ISMAC=1
fi
while getopts ":l:r:d:p:hx" opt; do
case $opt in
l)
USERNAME=$OPTARG
;;
d)
DEVICE=$OPTARG
;;
r)
HOSTNAME=$OPTARG
;;
x)
DISABLE=1
;;
p)
PORT=$OPTARG
;;
h)
usage
exit 0
;;
\?)
usage
exit 1
;;
esac
done
if [[ -z $DEVICE ]]
then
echo "Error: No network device specified.\n"
usage
exit 1
fi
if [[ $DISABLE -eq 1 ]];
then
echo 'disabling ssh tunnel, and SOCKS proxy.'
if [[ 1 -eq $ISMAC ]];
then
networksetup -setsocksfirewallproxystate $DEVICE off
fi
PID=$(ps aux |grep 'ssh -D 8080 -f -C -q -N' | grep -v 'grep' | head -n1 | awk '{ print $2 }')
if [ ! -z $PID ];
then
kill -9 $PID
fi
exit 0
fi
if [[ -z $USERNAME ]] || [[ -z $HOSTNAME ]]
then
echo "Error: Hostname and username must be specified.\n"
usage
exit 1
fi
echo 'creating ssh tunnel...'
ssh -D $PORT -f -C -q -N $USERNAME@$HOSTNAME
if [ 0 -eq $? ];
then
echo 'ssh tunnel create succesfully.'
else
echo 'host down or wrong password.'
exit 1
fi
if [ 1 -eq $ISMAC ];
then
networksetup -setsocksfirewallproxy $DEVICE 127.0.0.1 $PORT off
fi
echo "SOCKS proxy activated succesfully on device: $DEVICE"
exit 0