-
Notifications
You must be signed in to change notification settings - Fork 0
/
openvpn_random_port.sh
137 lines (137 loc) · 2.67 KB
/
openvpn_random_port.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
#!/bin/bash
###
ip=
sshuser=root
sshport=122
serverip=192.168.2.1
clientip=192.168.2.2
keyfile=s.key
app='openvpn'
salt='zM0miepRzgSk4yI8'
###
function random_port_generator (){
while true
do
rad=$RANDOM
if [ $rad -lt 65535 ]
then
cat>client.conf<<EOF
remote $ip $rad
dev tun
;proto tcp-client
ifconfig $clientip $serverip
secret $keyfile
comp-lzo
verb 3
redirect-gateway def1
;tun-mtu 7500
scramble obfuscate <$salt>
EOF
cat>server.conf<<EOF
dev tun
port $rad
;proto tcp-server
ifconfig $serverip $clientip
secret $keyfile
;keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
push "redirect-gateway def1"
push "dhcp-option DNS 8.8.8.8"
scramble obfuscate <$salt>
;tun-mtu 7500
EOF
fi
break
done
echo "Port change to:$rad"
echo "Port change to:$rad" >> /mnt/vpnlogs
}
function restart_vpn_server (){
echo Uploading file....
ssh -p $sshport $sshuser@$ip "killall $app"
scp -P $sshport server.conf $sshuser@$ip:/etc/openvpn
ssh -p $sshport $sshuser@$ip "$app --cd /etc/openvpn --config server.conf >> /dev/null &"
date=`date "+%Y-%m-%d %T"`
echo "Server Restarting..... $date" >> /mnt/vpnlogs
echo "Server Restarting..... $date"
}
function restart_vpn_client (){
killall $app
cp client.conf /etc/openvpn
$app --cd /etc/openvpn --config client.conf >> /dev/null &
date=`date "+%Y-%m-%d %T"`
echo "Client Restarting..... $date" >> /mnt/vpnlogs
echo "Client Restarting..... $date"
}
if [ $# = 1 ]
then
if [ $1 = 999 ]
then
echo Generating new port...
random_port_generator
restart_vpn_server
sleep 1
restart_vpn_client
sleep 1
fi
fi
vpn_status=`ifconfig |grep $clientip`
if [ -z "$vpn_status" ]
then
echo NO VPN Running,Starting VPN....
$app --cd /etc/openvpn --config client.conf >> /dev/null &
sleep 1
echo VPN is running
fi
while true
do
ping=`ping -c 5 $serverip |grep received |cut -b 24`
if [ $ping -eq 0 ]
then
((count+=1))
if [ $count -lt 4 ]
then
restart_vpn_client
sleep 5
ping=`ping -c 5 $serverip |grep received |cut -b 24`
else
while true
do
random_port_generator
restart_vpn_server
sleep 5
restart_vpn_client
sleep 5
ping=`ping -c 5 $serverip |grep received |cut -b 24`
echo $ping
if [ $ping -eq 0 ]
then
sleep 5
else
date=`date "+%Y-%m-%d %T"`
echo "VPN Restarted! Port:$rad $date" >> /mnt/vpnlogs
echo "VPN Restarted! Port:$rad $date"
x=1
break
fi
done
fi
if [ $ping -eq 0 ]
then
sleep 5
else
date=`date "+%Y-%m-%d %T"`
echo "VPN Restarted! $date" >> /mnt/vpnlogs
echo "VPN Restarted! $date"
fi
else
date=`date "+%Y-%m-%d %T"`
echo "VPN is OK $date" >> /mnt/vpnlogs
echo "VPN is OK $date"
fi
sleep 3
done