-
Notifications
You must be signed in to change notification settings - Fork 1
/
myvpn-setup
executable file
·67 lines (57 loc) · 2.67 KB
/
myvpn-setup
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
#!/bin/bash
function announce() {
echo -e "\n\n\n***************************************"
echo -e "$1"
echo "***************************************"
}
function err() {
echo -e "Error:" $* 1>&2
}
if [ ! -f "./.myvpn-create-server.sh" ]; then
err "Invalid setup"
exit 11
elif [ $# -ne 0 ]; then
err "This does not accept arguments!\nUsage:\n\t<parameters & values> ./myvpn-setup"
exit 21
elif [ "$REMOTE_HOSTNAME" = "" ]; then
err "Remote hostname is not specified. Set variable 'REMOTE_HOSTNAME'."
exit 31
elif [ "$REMOTE_USER" = "" ]; then
err "Remote username is not specified. Set variable 'REMOTE_USER'."
exit 32
elif [ "$REMOTE_CONFIG_DIR" = "" ]; then
err "Location of config directory in remote is not specified. Set variable 'REMOTE_CONFIG_DIR'."
exit 33
elif [ "$LOCAL_CONFIG_DIR" = "" -o ! -d "$LOCAL_CONFIG_DIR" ]; then
err "Location of config directory in local is either not specified or doesn't exist. Set variable 'LOCAL_CONFIG_DIR' to a valid/writable directory path"
exit 34
elif [ "$VPN_SERVER_NAME" = "" ]; then
err "Name for the VPN server is not specified. Set variable 'VPN_SERVER_NAME'."
exit 35
fi
set -e
announce "Copying OpenVPN server creation script to remote machine '$REMOTE_HOSTNAME'"
ssh $REMOTE_USER@$REMOTE_HOSTNAME "mkdir -p $REMOTE_CONFIG_DIR"
scp ./.myvpn-create-server.sh $REMOTE_USER@$REMOTE_HOSTNAME:$REMOTE_CONFIG_DIR
announce "Executing OpenVPN server creation script at remote machine"
ssh $REMOTE_USER@$REMOTE_HOSTNAME "cd $REMOTE_CONFIG_DIR; ./.myvpn-create-server.sh"
announce "Downloading key from remote machine to local machine (at location $LOCAL_CONFIG_DIR)"
scp $REMOTE_USER@$REMOTE_HOSTNAME:$REMOTE_CONFIG_DIR/static.key $LOCAL_CONFIG_DIR/$VPN_SERVER_NAME.key
announce "Creating client configuration file with name '$VPN_SERVER_NAME.conf'"
echo "remote $REMOTE_HOSTNAME
dev tun
ifconfig 10.8.0.2 10.8.0.1
secret $LOCAL_CONFIG_DIR/$VPN_SERVER_NAME.key
redirect-gateway def1
comp-lzo
keepalive 10 60
ping-timer-rem
persist-tun
persist-key" > $LOCAL_CONFIG_DIR/$VPN_SERVER_NAME.conf
announce "Removing shell script at remote"
ssh $REMOTE_USER@$REMOTE_HOSTNAME "rm $REMOTE_CONFIG_DIR/.myvpn-create-server.sh"
announce "Creating shell scripts at local (for starting and stopping VPN server at remote)"
echo "ssh $REMOTE_USER@$REMOTE_HOSTNAME \"cd $REMOTE_CONFIG_DIR; sudo openvpn --daemon --config server.conf\"" > "$LOCAL_CONFIG_DIR/start-$VPN_SERVER_NAME.sh"
chmod u+x "$LOCAL_CONFIG_DIR/start-$VPN_SERVER_NAME.sh"
echo "ssh $REMOTE_USER@$REMOTE_HOSTNAME \"sudo pgrep openvpn | xargs sudo kill -9\"" > "$LOCAL_CONFIG_DIR/stop-$VPN_SERVER_NAME.sh"
chmod u+x "$LOCAL_CONFIG_DIR/stop-$VPN_SERVER_NAME.sh"