Home Autowgc.sh - CLI only WireGuard Client for VPN Automation and Integration
Post
Cancel

Autowgc.sh - CLI only WireGuard Client for VPN Automation and Integration

Autowgc.sh is a wireguard client for linux written in bash/shell for automation of wireguard tunnels within a linux system. Allows for simple to advanced virtual networking setups to be created, called and then executed within C, bash, or perl.

Download:

1
sudo wget -qO - api.serverboi.org > ~/autowgc.sh

Execute:

1
sudo bash ~/autowgc.sh

Syntax:

sudo bash ~/autowgc [-h |-a file |-s ]

Flags:

-h Shows command syntax and other info.

-a file Add a wireguard client config file or ommit the file to start an already added connection. file Should be the location of a valid wireguard config:

1
sudo ~/autowgc -a /file.conf

-s Stop the wireguard VPN easily.



Source:

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
 #!/bin/bash
if (( EUID != 0 )); then
   echo "Error: Must be ran as root user" 1>&2
   exit 100
fi
WG=$(wg)
while getopts ":ahs" option; do
   case $option in
      a) 
         ;;
      h) 
   echo -e "autowgc (Auto wireguard client) - allows for easy managment of a wireguard connection."
   echo
   echo "Syntax: ./autowgc [-h |-a <file>| -s ]"
   echo "options:"
   echo "-a <file>    Add a wireguard client config file or ommit the <file> to start an already added connection."
   echo "-s           Stop the wireguard VPN."
   echo "-h           Shows command syntax and other info."
   echo
         exit;;
           s) 
if [ -z "$WG" ]
then
echo "Error: nothing to do: no active connection" && exit
else
systemctl stop [email protected] && echo -e "Stopped!" && exit 0
fi
###

         ;;
     \?) 
         echo "Error: Invalid or unreconized option"
         exit;;
                 
   esac
done
if [ -z "$1" ]
then
      echo "Error: nothing to do: no flags used" ; exit
else
      :
fi
if [ -z "$2" ]
then
      :
else
    cp -r $2 /etc/wireguard/wg0.conf  
fi
CONFIG=/etc/wireguard/wg0.conf
if [ -f "$CONFIG" ]; then
    :
else 
    echo "Error: $CONFIG does not exist." && exit 0
fi
###############################
echo "Starting connection!"
###############################
os_version=$(lsb_release -r |cut -f2)
localnetint=$(ip addr | awk '/state UP/ {print $2}'| tr -d ':' | tail -1)
if [ "$os_version" = "18.04" ]; then
    add-apt-repository universe 2>/dev/null
else
:
fi
UFW=$(ufw status verbose)
if [ "$UFW" = "Status: active" ]; then
  ufw route allow in on wg0 out on $localnetint|head -n 1
  ufw route allow in on $localnetint|head -n 1 out on wg0
else
:
fi
spinner()
{
    local pid=$!
    local delay=0.75
    local spinstr='|/-\'
    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
        local temp=${spinstr#?}
        printf " [%c]  " "$spinstr"
        local spinstr=$temp${spinstr%"$temp"}
        sleep $delay
        printf "\b\b\b\b\b\b"
    done
    printf "    \b\b\b\b"
}
(apt-get update > /dev/null && apt-get install openresolv -y > /dev/nulll && apt-get install wireguard -y > /dev/null && apt-get upgrade > /dev/null && wait && cd /etc/wireguard ; umask 077 && wg genkey > private-key && wg pubkey > public-key < private-key) &
spinner
###
systemctl start [email protected] 2>/dev/null 
if [ $? -eq 0 ]; then
    echo -e "Started:$(systemctl status [email protected] | grep SUCCESS | head -1)" 
else
    echo -e "Error: $(sudo journalctl -xe | grep "[email protected] has finished with a failure" | tail -1)"
	systemctl status [email protected] | grep "Main process exited, code=exited, status=1/FAILURE"
fi
exit 0


Execute within C:

1
2
3
#include <stdlib.h>

system("sudo bash ~/autowgc -a <file>");

Execute within C++:

1
2
3
#include <cstdlib>

std::system("sudo bash ~/autowgc -a <file>");

Execute within C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public string RunCommandWithBash(sudo bash ~/autowgc)
{
    var psi = new ProcessStartInfo();
    psi.FileName = "/bin/bash";
    psi.Arguments = -a <file>;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    using var process = Process.Start(psi);

    process.WaitForExit();

    var output = process.StandardOutput.ReadToEnd();

    return output;
}
// or try out CliWrap https://github.com/Tyrrrz/CliWrap for bash interaction from within C#

Execute within Perl:

1
2
$command="ls /";
system($sudo bash autowgc -a <file>);

Edit this page’s markdown on github.


This post is licensed under GNU GPL-3.0 .
Contents