wireguard interface configuration

If you have some wireguard interface to configure with a huge number of AllowedIPs, the route lines in the coresponding interface configuration can be gererated using a small script in order to avoid typing it by hand:

WGIFACE=wgtest;
WGGATEWAY=192.168.100.1;
for n in $(sed '/AllowedIPs/!d;s/.*= *//g;s/, */ /g'  /etc/wireguard/${WGIFACE}.conf); do
    echo -e "\tpost-up ip route add $n via $WGGATEWAY dev \$IFACE\n\tpre-down ip route del $n via $WGGATEWAY dev \$IFACE";
done

The lines gererated should be added to the interface configuration:

iface wgtest inet static
    address 192.168.100.200
    netmask 255.255.255.0
    pre-up ip link add $IFACE type wireguard
    pre-up wg setconf $IFACE /etc/wireguard/$IFACE.conf
    # insert routing lines here
    post-down ip link del $IFACE

BTW: the metric defined in wireguard route metric will apply to all routing lines. If you do not define a metric per interface, you can set a metric per routing line by appending a metric definition to the routing line:

post-up ip route add ... metric 900

linux
vpn
wireguard
interface
routing