-
Notifications
You must be signed in to change notification settings - Fork 17
/
autovpn.sh
executable file
·142 lines (105 loc) · 3.18 KB
/
autovpn.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
#!/usr/bin/env bash
#Author: Bakhtiyar Syed
#Institution: IIIT Hyderabad
#Date: 28 November 2016
#interact
chmod +x $0
usr=$1;
passwd=$2;
# Run in root if not already running in it
if [[ $(whoami) != "root" ]]; then
xhost +SI:localuser:root
sudo "$0" "$@"
xhost -SI:localuser:root
exit
fi
command -v zenity >/dev/null 2>&1 || {
if command -v apt-get 2&>1; then
apt-get update; apt-get install -y zenity;
elif command -v dnf 2&>1; then
dnf install -y zenity
fi
}
#[ -z $usr ] && read -p "Enter username: " -a usr
#[ -z $passwd ] && read -s -p "Enter password: " -a passwd
[ -z $usr ] && usr="$(zenity --entry --height=160 --width=400 --text="Enter your IIIT-H email ID" --title=Authentication)"
[ -z $passwd ] && passwd="$(zenity --password --height=160 --width=400 --text="Please enter your password" --title=Authentication)"
cd;
command -v openvpn >/dev/null 2>&1 || {
if command -v apt-get 2&>1; then # Ubuntu based distros
apt-get update; apt-get install -y openvpn;
elif command -v dnf 2&>1; then # Fedora based distros
dnf install -y openvpn
fi
}
command -v expect >/dev/null 2>&1 || {
if command -v apt-get 2&>1; then # Ubuntu based distros
apt-get update; apt-get install -y expect;
elif command -v dnf 2&>1; then # Fedora based distros
dnf install -y expect
fi
}
if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf";
then
sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf
fi
#apt-get update;
cd /etc/openvpn;
#rm -rf *
if [ -e "ca.crt" ];
then
rm -f ca.crt
fi
wget https://vpn.iiit.ac.in/ca.crt
if [ -e "all.iiit.ac.in.crt" ] || [ -e "all.iiit.ac.in.crt.*" ];
then
rm -f all.iiit.ac.in.crt;
rm -f all.iiit.ac.in.crt.*;
fi
wget https://vpn.iiit.ac.in/all.iiit.ac.in.crt
if [ -e "all.iiit.ac.in.key" ];
then
rm all.iiit.ac.in.key
fi
curl -O --user "$usr":"$passwd" https://vpn.iiit.ac.in/secure/all.iiit.ac.in.key
#fi
chmod 600 all.iiit.ac.in.key;
if [ -e "linux_client.conf" ];
then
rm -f linux_client.conf;
fi
wget https://vpn.iiit.ac.in/linux_client.conf
# Escape dollars in usr and passwd for expect's send
usr=$(echo "$usr"| sed 's/\$/\\\$/g')
passwd=$(echo "$passwd"| sed 's/\$/\\\$/g')
#Remove newline chars
#dt=${dt//$'\n'/}
#dt=${dt//$'\n'/}
usr="${usr//$'\\n'/}"
passwd="${passwd//$'\\n'/}"
#passwd="$(echo "$passwd" | sed -e 's/\n//g')"
#echo $passwd
expect <<- DONE
spawn openvpn --config linux_client.conf;
expect "Enter Auth Username:" { send "$usr\r" }
expect "Enter Auth Password:" { send "$passwd\r" }
expect "Initialization Sequence Completed"
interact;
DONE
sleep 12;
if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf";
then
echo "Nameserver already set. No need for further setting up";
else
sed -i '1i\'"nameserver 10.4.20.204" /etc/resolv.conf;
fi
if ! ping -c1 moodle.iiit.ac.in &>/dev/null
then
zenity --error --height=100 --width=400 --title="An Error Occurred" --text="VPN failed to start. Contact the administrator or see troubleshooting on github.com/flyingcharge/AutoVPN"
else
zenity --info --title="SUCCESS" --text="VPN SUCCESSFULLY RUNNING!"
fi
sleep 3;
#num=`history | tail -n 2 | head -n 1 | tr -s ' ' | cut -d ' ' -f 2`;
#history -c;
#export HISTFILE=/dev/null