-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwireguard_toggle.sh
executable file
·91 lines (77 loc) · 2.12 KB
/
wireguard_toggle.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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/wireguard_toggle.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2023-05-28T21:35:25+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
systemd_network="/etc/systemd/network"
wireguard_config="$systemd_network/wireguard"
prefix="99-"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to enable/disable wireguard interfaces
Usage:
$script [-s/--status] <interface>
Settings:
[-s/--status] = shows the status of the specified interface/config
<interface> = wireguard interface/config
Examples:
$script wg0
$script -s wg0
Config:
systemd_network = \"$systemd_network\"
wireguard_config = \"$wireguard_config\"
prefix = \"$prefix\""
check_root() {
[ "$(id -u)" -ne 0 ] \
&& printf "this script needs root privileges to run\n" \
&& exit 1
}
check_config() {
! [ -e "$wireguard_config/$prefix$1.netdev" ] \
&& printf "couldn't find config for %s\n" "$1" \
&& exit 1
}
enable_interface() {
ln -s "$wireguard_config/$prefix$1.netdev" \
"$systemd_network/$prefix$1.netdev"
ln -s "$wireguard_config/$prefix$1.network" \
"$systemd_network/$prefix$1.network"
systemctl restart systemd-networkd.service
printf "%s enabled\n" "$1"
}
disable_interface() {
ip link set "$1" down
ip link del dev "$1"
rm "$systemd_network/$prefix$1.netdev" \
"$systemd_network/$prefix$1.network"
printf "%s disabled\n" "$1"
}
interface_status() {
if [ -h "$systemd_network/$prefix$1.netdev" ]; then
printf "%s is enabled\n" "$1"
else
printf "%s is disabled\n" "$1"
fi
}
case "$1" in
-h | --help | "")
printf "%s\n" "$help"
;;
-s | --status)
check_config "$2"
interface_status "$2"
;;
*)
check_root
check_config "$1"
if [ -h "$systemd_network/$prefix$1.netdev" ]; then
disable_interface "$1"
else
enable_interface "$1"
fi
;;
esac