-
Notifications
You must be signed in to change notification settings - Fork 31
/
smartvpn.sh
347 lines (320 loc) · 11.5 KB
/
smartvpn.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
echo "┌──────────────────────────────────────────────────────────────────────┐"
echo "│ SMARTVPN │"
echo "├──────────────────────────────────────────────────────────────────────┤"
echo "│SMARTVPN IS A SHELL SCRIPT THAT HELP OPS SETUP OPENVPN SERVICE EASIER │"
echo "│ │"
echo "│ VERSION : 0.0.1 │"
echo "│ │"
echo "│SCRIPT INCLUDE: │"
echo "│ lzo-2.03 INSTALL /usr/local CONFIG null │"
echo "│ openssl-1.0.2k INSTALL /usr/local/openssl CONFIG null │"
echo "│ openvpn-2.1_rc22 INSTALL /usr/local/openvpn CONFIG /etc/openvpn │"
echo "│ smartvpn-0.0.1 INSTALL /usr/local/smartvpn CONFIG null │"
echo "│ │"
echo "│SCRIPT DEPEND: │"
echo "│ linux expect tool │"
echo "│ │"
echo "│QUICK USE │"
echo "│ /etc/init.d/openvpn start/stop │"
echo "│ /usr/local/smartvpn/smartvpn #interactive │"
echo "│ │"
echo "│ PoweredBy GyyxOpTeam │"
echo "└──────────────────────────────────────────────────────────────────────┘"
workdir=/usr/local/smartvpn
openvpnport=10050
openserveriparea="172.31.0.0 255.255.255.0"
#server software install
serversoftinstall(){
cd $workdir
echo "SETUP LZO.."
cd smartvpn-package
tar -zxvf lzo-2.03.tar.gz
cd lzo-2.03
./configure -prefix=/usr/local && make && make install
cd ..
rm -rf lzo-2.03
echo "SETUP OPENSSL.."
tar -zxvf openssl-1.0.2k.tar.gz
cd openssl-1.0.2k
./config -prefix=/usr/local/openssl && make && make install
cd ..
rm -rf openssl-1.0.2k
echo "SETUP OPENVPN.."
tar -zxvf openvpn-2.1_rc22.tar.gz
cd openvpn-2.1_rc22
./configure -prefix=/usr/local/openvpn && make && make install
cd ..
rm -rf openvpn-2.1_rc22
}
#client software install
clientsoftinstall() {
echo "REMOTE SETUP LZO.."
expect <<!
set timeout 120
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "cd /usr/local/smartvpn/smartvpn-package/ && tar -zxvf lzo-2.03.tar.gz && cd lzo-2.03 && ./configure -prefix=/usr/local && make && make install && cd .. && rm -rf lzo-2.03"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
echo "REMOTE SETUP OPENSSL"
expect <<!
set timeout 200
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "cd /usr/local/smartvpn/smartvpn-package/ && tar -zxvf openssl-1.0.2k.tar.gz && cd openssl-1.0.2k && ./config -prefix=/usr/local/openssl && make && make install && cd .. && rm -rf openssl-1.0.2k"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
echo "REMOTE SETUP OPENVPN"
expect <<!
set timeout 200
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "cd /usr/local/smartvpn/smartvpn-package/ && tar -zxvf openvpn-2.1_rc22.tar.gz && cd openvpn-2.1_rc22 && ./configure -prefix=/usr/local/openvpn && make && make install && cd .. && rm -rf openvpn-2.1_rc22"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
}
serverinit() {
echo "--------OPENVPN SERVER PREPARE TO SETUP----------"
echo "SYSTEM INIT.."
sed -i '/net.ipv4.ip_forward/ s/\(.*= \).*/\11/' /etc/sysctl.conf
sysctl -p
serversoftinstall
cd $workdir
echo "CONFIG INIT.."
mkdir -r /etc/openvpn
cd smartvpn-package
tar -zxvf openvpn-config.tar.gz
cp -R ./openvpn-config /etc/openvpn
rm -rf openvpn-config
cd ..
cp -r ./smartvpn-package/init.d.openvpn /etc/init.d/openvpn
sed -i "s/client/server/g" /etc/init.d/openvpn
chmod +x /etc/init.d/openvpn
#rm /etc/openvpn/client.conf
cd /etc/openvpn/easy-rsa/
source ./vars
./clean-all
expect <<!
set timeout 10
spawn ./build-ca
expect {
"]" { send "\r"; exp_continue}
}
!
echo "CA CREATE SEUCCESS!"
#sleep 2
expect <<!
set timeout 20
spawn ./build-key-server server
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "y\r"
expect "*"
send "y\r"
expect eof
!
echo "SERVER KEY CREATE SUCCESS!"
#sleep 3
./build-dh
#mkdir /etc/openvpn/server_key
cp ./keys/server.* /etc/openvpn/keys
cp ./keys/*.pem /etc/openvpn/keys
cp ./keys/ca* /etc/openvpn/keys
sed -i "s/#LOCALIP#/$SERVERIP/g" /etc/openvpn/server.conf
sed -i "s/#PORT#/$PORT/g" /etc/openvpn/server.conf
sed -i "s/#SERVERIPAREA#/$SERVERIPAREA/g" /etc/openvpn/server.conf
sed -i "s/#SERVERIP#/$SERVERIP/g" /etc/openvpn/client-conf/client.conf
sed -i "s/#PORT#/$PORT/g" /etc/openvpn/client-conf/client.conf
}
clientinit() {
echo "--------OPENVPN CLIENT PREPARE TO SETUP----------"
echo "SYSTEM INIT.."
expect <<!
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "sed -i '/net.ipv4.ip_forward/ s/\\\(.*= \\\).*/\\\11/' /etc/sysctl.conf && sysctl -p"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
clientsoftinstall
cd /etc/openvpn/easy-rsa/
source ./vars
expect <<!
spawn ./build-key CLIENT_$CLIENTID
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "y\r"
expect "*"
send "y\r"
expect eof
!
mkdir -p /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys
cp ./keys/CLIENT_$CLIENTID* /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys
cp ./keys/ca.crt /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys
cp /etc/openvpn/client-conf/client.conf /etc/openvpn/client-conf/CLIENT_$CLIENTID/
mv /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/CLIENT_$CLIENTID.crt /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/client.crt
mv /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/CLIENT_$CLIENTID.csr /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/client.csr
mv /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/CLIENT_$CLIENTID.key /etc/openvpn/client-conf/CLIENT_$CLIENTID/keys/client.key
expect <<!
spawn scp -r -P $CLIENTPORT $workdir/smartvpn-package/init.d.openvpn root@$CLIENTIP:/etc/init.d/openvpn
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
expect <<!
set timeout 20
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "mkdir -r /etc/openvpn"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
expect <<!
spawn scp -r -P $CLIENTPORT /etc/openvpn/client-conf/CLIENT_$CLIENTID root@$CLIENTIP:/etc/openvpn
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
expect <<!
set timeout 20
spawn ssh -p $CLIENTPORT -t root@$CLIENTIP "chmod +x /etc/init.d/openvpn"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
}
preparechech(){
echo "PREPARE CHECKING.."
if [ `rpm -qa | grep expect |wc -l` -eq 0 ];then
echo "[FAIL]EXPECT PACKAGE IS NOT INSTALL , PLEASE INSTALL EXPECT FIRST!(exp:yum install expect)"
exit
fi
}
sysinit(){
echo "SYSTEM INIT.."
sed -i '/net.ipv4.ip_forward/ s/\(.*= \).*/\11/' /etc/sysctl.conf
sysctl -p
echo "[SUCCESS]SET IP FORWARD OK !"
}
#-------------------------------------MAIN----------------------------------------------
preparechech
read -p "WHICH SERVICE YOU WANT SETUP?( 1:SERVER,2:CLIENT ):" SOC
if [[ $SOC == "1" ]];then
while :
do
read -p "PLEASE ENTER YOUR SERVER IP ADDR(THIS ADDR USE FRO CLIENT CONNECT):" SERVERIP
regex="\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\b"
ckStep2=`echo $SERVERIP | egrep $regex | wc -l`
if [ $ckStep2 -eq 0 ]
then
echo "[FAIL]THE STRING $SERVERIP IS NOT A CORRECT IPADDR,PLEASE TRY AGAIN!"
else
break
fi
done
read -p "PLEASE ENTER YOUR OPENVPN PORT (DEFAULT:10050):" PORT
if [ ! -n "$PORT" ];then
PORT=$openvpnport
fi
read -p "PLEASE ENTER YOUR OPENVPN COMMUNICATION SUBNET (DEFAULT:172.31.0.0 255.255.255.0):" SERVERIPAREA
if [ ! -n "$SERVERIPAREA" ];then
SERVERIPAREA=$openserveriparea
fi
echo " VERFIY YOUR CONFIGRATION! "
echo " "
echo " SERVER IP $SERVERIP "
echo " OPENVPN PORT $PORT "
echo " OPENVPN COMMUNICATION SUBNET $SERVERIPAREA "
echo " "
read -p "PLEASE VIRFY YOUR CONFIG?( y/n ):" BEGIN
if [[ $BEGIN != "y" ]];then
exit
fi
rm -rf /usr/local/smartvpn
mkdir /usr/local/smartvpn
cp -rf smartvpn.sh /usr/local/smartvpn
cp -rf smartvpn-package /usr/local/smartvpn
#begin to install server
serverinit
echo "[SUCCESS]OPENVPN SERVICE SETUP FINISH,YOU CAN FIND THIS SCRIPT AT /usr/local/smartvpn"
read -p "WOULD YOU LIKE TO START OPENVPN NOW?( y/n )" STARTSERVICE
if [[ $STARTSERVICE == "y" ]];then
/etc/init.d/openvpn start
fi
elif [[ $SOC == "2" ]];then
echo "NOTICE!! YOU MUST RUN THIS SCRIPT AT OPENVPN SERVER OR IT WILL FAIL!"
SERVERIPADDR=`cat /etc/openvpn/server.conf | grep local | awk {'print $2'}`
SERVERPORT=`cat /etc/openvpn/server.conf | grep port | awk {'print $2'}`
read -p "PLEASE ENTER YOUR CLIENT SSH IP ADDR:" CLIENTIP
read -p "PLEASE ENTER YOUR CLIENT SSH PORT:" CLIENTPORT
read -p "PLEASE ENTER YOU CLIENT ROOT PASSWORD:" ROOTPWD
read -p "PLEASE ENTER YOU CLIENT ID(demo:001):" CLIENTID
echo " VERFIY YOUR CONFIGRATION! "
echo " "
echo " SERVER IP:PORT $SERVERIPADDR:$SERVERPORT "
echo " CLIENT IP $CLIENTIP "
echo " CLIENT ID $CLIENTID "
echo " "
read -p "PLEASE VIRFY YOUR CONFIG?( y/n ):" BEGIN
if [[ $BEGIN != "y" ]];then
exit
fi
#scp software package to client
expect <<!
spawn scp -r -P $CLIENTPORT /usr/local/smartvpn root@$CLIENTIP:/usr/local
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
#begin to install client
clientinit
echo "[SUCCESS]OPENVPN SERVICE SETUP FINISH,YOU CAN FIND THIS SCRIPT AT /usr/local/smartvpn"
read -p "WOULD YOU LIKE TO START OPENVPN CLIENT NOW?( y/n )" STARTSERVICE
if [[ $STARTSERVICE == "y" ]];then
expect <<!
spawn ssh -t -p $CLIENTPORT root@$CLIENTIP "/etc/init.d/openvpn start"
expect {
"password" { send "$ROOTPWD\r"; exp_continue}
}
!
fi
else
echo "[FAIL]Param Error! PLEASE TRY AGAINE!"
fi