-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn-auto-auth.sh
executable file
·57 lines (42 loc) · 1.27 KB
/
vpn-auto-auth.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
#!/bin/bash
############################################# FUNCTIONS
create-auth-file() {
echo "Enter your credentials..."
echo -n "Username: "
read username
echo -n "Password: "
read -s password
echo
touch /etc/openvpn/auth.txt
echo $username >> /etc/openvpn/auth.txt
echo $password >> /etc/openvpn/auth.txt
}
############################################## START OF SCRIPT
# only let this script run in root mode
if [ "$EUID" -ne 0 ]; then
echo 'Please run as root'
exit
fi
# ensure there is a valid file path (most likely to a .ovpn file) as an argument
if [ -z "$1" -o ! -f "$1" ]; then
echo 'Please pass an ovpn file as an argument'
exit
fi
# Step 1: check if auth file exists
echo -n "Checking if auth.txt exists..."
if [ -f /etc/openvpn/auth.txt ]; then
echo "it exists!"
else
echo "it doesn't"
create-auth-file
fi
# Step 2: check if auth-user-pass exists in the file, then inject the auth file
echo -n "Injecting option into .ovpn file..."
if grep -Fxq "auth-user-pass" "$1"; then
# sed is magical
sed -i 's@auth-user-pass$@auth-user-pass /etc/openvpn/auth.txt@' "$1"
echo "done"
else
echo
echo 'ERROR: Input file does not contain the option auth-user-pass (or it has already been assigned)'
fi