View difference between Paste ID: jvkc9bf9 and x3i0QYFg
SHOW: | | - or go back to the newest paste.
1
#!/bin/sh
2
#=======================================================
3
# create_dual_ovpn_ddwrt.sh  - v1.1.0
4
#=======================================================
5
# Changelog
6
#=======================================================
7
# v1.1.0 - 02/20/2012 - Kain0o0
8
# - Changed name guessing to match new Strongvpn script
9-
ip rule add from 192.168.1.100\/30 lookup 4\n\
9+
# - Created DUAL_SCRIPT variable for user preference
10-
ip rule add from 192.168.1.104\/29 lookup 4\n\
10+
# - Created clean_sed function to parse sed output
11-
ip rule add from 192.168.1.112\/28 lookup 4\n\
11+
# - Script is easily modified using clean_sed input
12-
ip rule add from 192.168.1.128\/25 lookup 4\n\
12+
# - Modified code and added comments for easier reading
13
# - Added brakets for notepad++ collapsing support
14
# - Added changelog to track changes
15
# v1.0.2 - 08/12/2011 - Cfelix (Thanks!)
16
# - Added upnp line
17
# - Missing \'s for $RULE.  Changed to echo \\\\$\RULE
18-
echo -e "\033[31m\033[47m                      CREATE_DUAL_OVPN_DDWRT                           \033[0m"
18+
# - Modified various sed commands
19
# v1.0.1 - 11/29/2010 - Kain0o0
20
# - Added PORT_RANGE variable for easier modification
21
# v1.0.0 - 11/25/2010 - Kain0o0
22
# - initial release
23
24
# Modify if you have a different DHCP port range
25-
echo -e "\033[34m\033[47m Input:  Original StrongVPN installer script (ovpnXXX_ddwrt.sh)        \033[0m"
25+
26-
echo -e "\033[34m\033[47m Output: Dual installation script            (ovpn_dual_ddwrt.sh)      \033[0m"
26+
27
# Please maintain format ip rule add from XXX.XXX.XXX.XXX\/XX lookup 4\n\
28
29-
GUESS=ovpn???_ddwrt.sh
29+
30
ip rule add from 192.168.1.100/30 lookup 4
31
ip rule add from 192.168.1.104/29 lookup 4
32-
  echo Is this your file : $GUESS
32+
ip rule add from 192.168.1.112/28 lookup 4
33-
  while true
33+
ip rule add from 192.168.1.128/25 lookup 4
34-
  do
34+
35-
  echo -n "Please confirm (y or n): "
35+
36-
  read CONFIRM
36+
# New script name, modify it if you have a naming preference
37-
  case $CONFIRM in
37+
DUAL_SCRIPT="ovpn_dual_ddwrt.sh"
38-
    y|Y|YES|yes|Yes)
38+
39-
      INSTALL_SCRIPT="$GUESS"
39+
#BANNER
40-
      break
40+
{
41-
    ;;
41+
42-
    n|N|no|NO|No)
42+
43-
      echo Please input the original installer script name:
43+
44-
      read INSTALL_SCRIPT
44+
echo -e "\033[31m\033[47m                      CREATE_DUAL_OVPN_DDWRT                     v1.1.0\033[0m"
45-
      if [ ! -z $INSTALL_SCRIPT ];then
45+
46-
        if [ ! -f $INSTALL_SCRIPT ];then
46+
47-
          echo File doesnt exist in this directory
47+
48-
          echo are you sure you copied all the files here?
48+
49-
          echo Check file name \& copy location then restart.
49+
50-
          exit
50+
51-
        fi
51+
echo -e "\033[34m\033[47m Input:  Original StrongVPN installer script (ovpnXXX_ddwrt_small.sh)  \033[0m"
52-
      else
52+
echo -e "\033[34m\033[47m Output: Dual installation script            ($DUAL_SCRIPT)      \033[0m"
53-
      echo Blank file name given, please retry!
53+
54-
      exit
54+
}
55-
      fi
55+
56-
      break
56+
# Get StrongVPN ddwrt script to modify it
57-
    ;;
57+
{
58-
    *) clear | echo Please enter only y or n
58+
# Tries to guess the StrongVPN script name
59-
  esac
59+
GUESS=ovpn???_ddwrt_small.sh
60-
  done
60+
61
if [ -f $GUESS ];then
62-
  echo Please input the original installer script name:
62+
echo Is this your file : $GUESS
63-
  read INSTALL_SCRIPT
63+
64-
  if [ ! -f $INSTALL_SCRIPT ];then
64+
65-
    echo File doesnt exist in this directory
65+
66-
    echo are you sure you copied all the files here?
66+
67-
    echo Check file name \& copy location then restart.
67+
68-
    exit
68+
y|Y|YES|yes|Yes)
69-
  fi
69+
INSTALL_SCRIPT="$GUESS"
70
break
71
;;
72-
cp $INSTALL_SCRIPT temp1
72+
73
echo Please input the original installer script name:
74
read INSTALL_SCRIPT
75
if [ ! -z $INSTALL_SCRIPT ];then
76-
sed 's/openvpn --daemon --config ovpn.conf/sh \/tmp\/etc\/config\/ovpn.wanup/'< temp1 >temp0
76+
if [ ! -f $INSTALL_SCRIPT ];then
77
echo File doesnt exist in this directory
78-
sed "s/echo -e \"#!\/bin\/sh\\\nkillall openvpn\\\nping -c4 localhost\\\nopenvpn --daemon --config \
78+
echo are you sure you copied all the files here?
79-
\/tmp\/ovpn\/ovpn.conf\">\/tmp\/etc\/config\/ovpn.wanup/\
79+
echo Check file name \& copy location then restart.
80-
echo -e \"\n\
80+
81-
#!\/bin\/sh\n\
81+
82-
killall openvpn\n\
82+
83-
ping -c4 localhost\n\
83+
echo Blank file name given, please retry!
84-
\n\
84+
85-
# Clean up by flushing table 4 and deleting all ip rules\n\
85+
86-
ip route flush table 4\n\
86+
break
87-
ip rule show | grep -Ev \\\\\"^(0|32766|32767)\\\\\" | while read PRIO RULE; do ip rule del prio \
87+
88-
\\\\$\{PRIO%%:*\} \\\\$\( echo \$RULE | sed \\\\\"s|all|0\/0|\\\\\" \); done\n\
88+
89-
\n\
89+
90-
# Create backup of default route table\n\
90+
91-
ip route show table main > \/tmp\/ovpn\/route.isp\n\
91+
92-
\n\
92+
echo Please input the original installer script name:
93-
# start openvpn\n\
93+
read INSTALL_SCRIPT
94-
openvpn --daemon --config \/tmp\/ovpn\/ovpn.conf\n\
94+
if [ ! -f $INSTALL_SCRIPT ];then
95-
\n\
95+
echo File doesnt exist in this directory
96-
# Add rules for all DHCP routes (192.168.1.100 -> 192.168.1.255)\n\
96+
echo are you sure you copied all the files here?
97-
$PORT_RANGE\
97+
echo Check file name \& copy location then restart.
98-
\n\
98+
99-
# Flush route cache\n\
99+
100-
ip route flush cache\n\
100+
101-
\n\
101+
}
102-
\\\">\/tmp\/etc\/config\/ovpn.wanup/" < temp0 > temp1
102+
103
# Creating working copy of script
104-
sed "s/nvram set ovpn_up='iptables -t nat -A POSTROUTING -o \$dev -j MASQUERADE/\
104+
cp $INSTALL_SCRIPT $DUAL_SCRIPT
105-
nvram set ovpn_up='iptables -t nat -A POSTROUTING -o \$dev -j MASQUERADE\n\n\
105+
106-
# Use original (pre-openvpn) route for table 4\n\
106+
107-
cat \/tmp\/ovpn\/route.isp | while read ROUTE; do ip route add table 4 \$ROUTE; done\n/" < temp1 > temp0
107+
108
# Cleans sed output, adds the necessary escape characters needed.
109-
sed "s/killall -HUP dnsmasq/\
109+
# Arguments: arg1= variable holding the replacing text
110-
killall -HUP dnsmasq\n\n\
110+
#            arg2(optional)=multi if the variable consists of multiple lines
111-
# Clean up by flushing table 4 and deleting all ip rules\n\
111+
clean_sed(){
112-
ip route flush table 4\n\
112+
pattern="$1"
113-
ip rule show | grep -Ev \\\\\"^(0|32766|32767)\\\\\" | while read PRIO RULE; do ip rule del prio \
113+
safe_pattern=$(echo "$pattern" | sed 's/[][\.*/]/\\&/g; s/$$/\\&/; s/^^/\\&/')
114-
\\\\$\{PRIO%%:*\} \\\\$\( echo \$RULE | sed \\\\\"s|all|0\/0|\\\\\" \); done\n/" < temp0 > temp1
114+
if [ $2 == "multi" ]; then
115
safe_pattern=$(echo "$safe_pattern" | sed 's/$/\\\\n/')
116-
echo Creating Reboot option....
116+
117-
sed  "s/nvram commit/\
117+
echo -e $safe_pattern
118-
nvram commit \n\
118+
}
119-
echo Press any key to reboot..... \n\
119+
120-
read dummy_var \n\
120+
# Modified script text - combined here to make future changes easier
121-
reboot \n\ /" < temp1 > ovpn_dual_ddwrt.sh
121+
{
122
#TEXT1
123-
rm temp0 temp1
123+
# Replaces openvpn --daemon --config ovpn.conf
124-
chmod +x ovpn_dual_ddwrt.sh
124+
{
125
TEXT1='sh /tmp/etc/config/ovpn.wanup'
126
}
127
128
#TEXT2
129
# The double slashes are necessary since this text will be ran through sed AND echo -e
130
# Modifies wanup script
131
{
132
TEXT2='echo -e "
133
#!/bin/sh
134
killall openvpn
135
upnp -D -W tun0
136
ping -c4 localhost
137
138
# Clean up by flushing table 4 and deleting all ip rules
139
ip route flush table 4
140
ip rule show | grep -Ev \\"^(0|32766|32767)\\" | while read PRIO RULE; do ip rule del prio \\${PRIO%%:*} $( echo \\$RULE | sed \\"s|all|0/0|\\" ); done
141
142
# Create backup of default route table
143
ip route show table main > /tmp/ovpn/route.isp
144
145
# start openvpn
146
openvpn --daemon --config /tmp/ovpn/ovpn.conf
147-
sh ovpn_dual_ddwrt.sh
147+
148
# Add rules for all DHCP routes (192.168.1.100 -> 192.168.1.255)
149
'$PORT_RANGE'
150
151
# Flush route cache
152
ip route flush cache
153
154
">/tmp/etc/config/ovpn.wanup'
155
}
156
157
#TEXT3
158
#  Modifies ovpn_up script
159
{
160
TEXT3='nvram set ovpn_up='"'"'iptables -t nat -A POSTROUTING -o $dev -j MASQUERADE
161
162
# Use original (pre-openvpn) route for table 4
163
cat /tmp/ovpn/route.isp | while read ROUTE; do ip route add table 4 $ROUTE; done'
164
}
165
166
#TEXT4
167
# Modifies ovpn_dn script
168
{
169
TEXT4='killall -HUP dnsmasq
170
171
# Clean up by flushing table 4 and deleting all ip rules
172
ip route flush table 4
173
ip rule show | grep -Ev "^(0|32766|32767)" | while read PRIO RULE; do ip rule del prio ${PRIO%%:*} $( echo $RULE | sed "s|all|0/0|" ); done'"'"' '
174
}
175
176
#TEXT5
177
# Add reboot option
178
{
179
TEXT5='nvram commit 
180
echo Press any key to reboot..... 
181
read dummy_var 
182
reboot'
183
}
184
}
185
186
# Do the actual modification using the text above
187
{
188
#TEXT1
189
# Replaces openvpn --daemon --config ovpn.conf
190
sed -i "s/openvpn --daemon --config ovpn.conf/`clean_sed "$TEXT1"`/" $DUAL_SCRIPT
191
192
#TEXT2
193
# Modifies wanup script
194
sed -i "s/echo -e.*/`clean_sed "$TEXT2" "multi"`/" $DUAL_SCRIPT
195
196
#TEXT3
197
#  Modifies ovpn_up script
198
sed -i "s/nvram set ovpn_up.*/`clean_sed "$TEXT3" "multi"`/" $DUAL_SCRIPT
199
200
#TEXT4
201
# Modifies ovpn_dn script
202
sed -i "s/killall -HUP dnsmasq.*/`clean_sed "$TEXT4" "multi"`/" $DUAL_SCRIPT
203
204
#TEXT5
205
# Add reboot option
206
sed  -i "s/nvram commit/`clean_sed "$TEXT5" "multi"`/" $DUAL_SCRIPT
207
208
#CLEANUP
209
# Remove blank spaces for clarity
210
sed -i 's/^ //' $DUAL_SCRIPT
211
}
212
213
# Make script executable
214
chmod +x $DUAL_SCRIPT
215
216
# Ask user if he wants to reboot
217
{
218
echo
219
echo All Done! 
220
echo
221
echo Would you like to start the OpenVPN DD-WRT install?
222
echo \(You can manualy install at a later time by running ovpn_dual_ddwrt.sh\)
223
while true
224
do
225
echo -n "Please confirm (y or n): "
226
read CONFIRM
227
case $CONFIRM in
228
y|Y|YES|yes|Yes) break ;;
229
n|N|no|NO|No)
230
echo Aborting - You can manualy install at a later time by running ovpn_dual_ddwrt.sh
231
exit
232
;;
233
*) clear | echo Please enter only y or n
234
esac
235
done
236
echo
237
echo Running the installation script
238
# Run newly created script
239
sh $DUAL_SCRIPT
240
}