-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_vpn.sh
executable file
·194 lines (162 loc) · 3.46 KB
/
start_vpn.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
##
# Author: Fernando Israél García Martínez
# mail: [email protected]
#
# Limitation: This script only runs in bash version 4 or higher.
#
# Es necesario e impepinable tener ya creada la configuración del tunnel:
# pptpsetup --create <nombre_tunnel> --server <server> --username <user> --password <password> --encrypt
#
#COMMAND="pon inlog debug dump logfd 2 noipdefault nodetach"
#SCREEN_OPTIONS="-S vpn_inlog -t vpn_inlog -m -d"
#screen ${SCREEN_OPTIONS} ${COMMAND}
#
#sleep 10
#ip r add 192.168.1.0/24 via 192.168.1.1 dev ppp0
#
#ip a show ppp0
#ip r
declare -a PIDS
RUN_FILE="/tmp/vpn_run_file"
PREFIX="vpn_inlog"
function techo(){
# Color Code
# Black 0
# Red 1
# Green 2
# Yellow 3
# Blue 4
# Magenta 5
# Cyan 6
# White 7
local COLOR=${1}
shift
tput setaf ${COLOR}
echo "$@"
tput sgr0
}
function error(){
case $1 in
1)
tput setaf 1
cat << __EOF__
There's a similar screen/process runnig, check it or remove ${RUN_FILE}.
__EOF__
tput sgr0
;;
2)
tput setaf 1
cat << __EOF__
There isn't ${RUN_FILE}, check your 'screen' execution.
__EOF__
tput sgr0
;;
esac
exit $1
}
function get_pids(){
local PID=$1
if [ $(ps h --ppid ${PID} -o pid) ]
then
get_pids $(ps h --ppid ${PID} -o pid)
PIDS+=(${PID})
elif [ "$(ps --pid ${PID} -o pid)" != "PID" ]
then
PIDS+=${PID}
fi
}
function start(){
[ -f ${RUN_FILE} ] && error 1
# Es necesario e impepinable tener ya creada la configuración del tunnel:
# pptpsetup --create <nombre_tunnel> --server <server> --username <user> --password <password> --encrypt
#
#### ----------- ####
ID="${PREFIX}_$(openssl rand -hex 4)"
COMMAND="pon inlog debug dump logfd 2 noipdefault nodetach"
SCREEN_OPTIONS="-S ${ID} -t ${ID} -m -d"
#### ---------- ####
techo 2 "Starting screen-vpn inLog process"
screen ${SCREEN_OPTIONS} ${COMMAND}
sleep 10
# Add the route that we need
ip route add 192.168.1.0/24 via 192.168.1.1 dev ppp0
echo "ID=${ID}" > ${RUN_FILE}
SCREEN=$(screen -ls | awk '/'${ID}'/ {print $1}')
echo "SCREEN=${SCREEN}" >> ${RUN_FILE}
echo "SCREEN_PID=${SCREEN%.*}" >> ${RUN_FILE}
techo 5 "status:"
status
}
function stop(){
[ ! -f ${RUN_FILE} ] && error 2
source ${RUN_FILE}
get_pids ${SCREEN_PID}
techo 1 "Stoping vpn screen process pid==${SCREEN_PID}"
kill -9 ${PIDS[*]}
screen -wipe
[ $? ] && rm ${RUN_FILE}
techo 5 "status:"
status
}
function status(){
techo 2 "screens UP"
screen -ls
[ ! -f ${RUN_FILE} ] && error 2
source ${RUN_FILE}
get_pids ${SCREEN_PID}
techo 3 "screen: ${SCREEN}"
techo 1 "pids : ${PIDS[*]}"
techo 2 -e "\nPIDs"
for PID in ${PIDS[*]}
do
ps h --pid ${PID}
done
techo 2 -e "\nVPN interface"
ip addr show ppp0
techo 2 -e "\nNetwork routes"
ip route show
}
function flush(){
# brute force
for SCREEN in $(screen -ls | awk '/'${PREFIX}'/ {print $1}')
do
get_pids ${SCREEN%.*}
techo 6 "Killing all screens ${PIDS[*]}"
kill -9 ${PIDS[*]}
done
[ $? ] && rm ${RUN_FILE}
sleep 5
screen -wipe
techo 4 "The rest screens ..."
screen -ls
}
function usage(){
cat << __EOF__
Script to start/stop/status VPN inLog process:
Usage:
$(pwd)/start_vpn.sh {start|stop|status|flush|help}
* start :Start VPN
* stop :Stop VPN
* status :Show status about "screens process".
* flush :It is a brute force cleaner killer of all VPN screens process.
__EOF__
}
ACTION=${1:-stop}
case $ACTION in
start)
start
;;
stop)
stop
;;
status)
status
;;
flush)
flush
;;
*)
usage
;;
esac