ungleich-tools/wireguard/gen-tunnels.sh

69 lines
1.2 KiB
Bash
Raw Normal View History

2021-12-31 15:03:13 +00:00
#!/bin/sh
# 2021-12-30
# Nico Schottelius
if [ $# -ne 7 ] ; then
echo $0 "v6|v4" vpngw vpnpubkey prefix mask start end
echo "f.i. $0 v4 vpn-....ungleich.ch:51820 6BRnQ.. 192.0.0. 32 22 43"
echo "f.i. $0 v6 vpn-....ungleich.ch:51820 6BRnQ.. 2a0a:e5c0: 48 22 333"
exit 1
fi
v4v6=$1; shift
vpngw=$1; shift
vpnpub=$1; shift
prefix=$1; shift
mask=$1; shift
start=$1; shift
end=$1; shift
case "$v4v6" in
v6)
sep=":"
allowed_ips="::/0"
2023-04-20 11:51:56 +00:00
gw_mask="/128"
2021-12-31 15:03:13 +00:00
;;
v4)
sep="."
allowed_ips="0.0.0.0/0"
2023-04-20 11:51:56 +00:00
gw_mask="/32"
2021-12-31 15:03:13 +00:00
;;
*)
echo "Unsupported, use v6 or v4" >&2
exit 1
;;
esac
: > gw.conf
for ip in $(seq $start $end); do
privkey=$(wg genkey)
pubkey=$(echo $privkey | wg pubkey)
addr=$prefix${sep}${ip}/${mask}
addr_nomask=$prefix${sep}${ip}
file="${addr_nomask}.conf"
2021-12-31 15:03:13 +00:00
echo "Writing ${file} and updating gw.conf"
cat <<EOF > $file
[Interface]
PrivateKey = ${privkey}
2021-12-31 15:03:13 +00:00
Address = ${addr}
[Peer]
PublicKey = ${vpnpub}
Endpoint = ${vpngw}
AllowedIPs = ${allowed_ips}
2021-12-31 15:03:13 +00:00
EOF
cat <<EOF >> gw.conf
[Peer]
PublicKey = ${pubkey}
2023-04-20 11:51:56 +00:00
AllowedIPs = ${addr_nomask}${gw_mask}
2021-12-31 15:03:13 +00:00
EOF
done