Skip to content

Instantly share code, notes, and snippets.

@cweiland
Last active May 14, 2021 15:28
Show Gist options
  • Save cweiland/ce8bb988d764216c44e6cc680c3fe56b to your computer and use it in GitHub Desktop.
Save cweiland/ce8bb988d764216c44e6cc680c3fe56b to your computer and use it in GitHub Desktop.
Create multiple meshnetworks config using wireguard
#!/bin/bash
declare -a meshnetworks
declare -A hosts
declare -A wg_port
declare -A wg_subnet
configdir="/tmp/wireguard"
sshport=21
meshnetworks=("net1" "net2" "net3")
wg_port[net1]=1655
wg_subnet[net1]=10.10.10.
wg_port[net2]=1656
wg_subnet[net2]=10.10.11.
wg_port[net3]=1657
wg_subnet[net3]=10.10.12.
hosts[host1]=1
hosts[host2]=2
hosts[host3]=3
mkdir ${configdir}
hostnames=(${!hosts[@]})
hostname_len=${#hostnames[@]}
for meshnetwork in ${meshnetworks[@]}
do
for (( i=0; i<${hostname_len}; i++ ))
do
wg genkey | tee ${configdir}/${meshnetwork}_private_${hostnames[i]} | wg pubkey > ${configdir}/${meshnetwork}_public_${hostnames[i]}
cat << EOF > ${configdir}/${meshnetwork}_${hostnames[i]}.conf
# ${meshnetwork} ${hostnames[i]} ${wg_subnet[${meshnetwork}]}${hosts[${hostnames[i]}]}
[Interface]
PrivateKey = `cat ${configdir}/${meshnetwork}_private_${hostnames[i]}`
ListenPort = ${wg_port[${meshnetwork}]}
EOF
done
for (( i=0; i<${hostname_len}; i++ ))
do
for (( j=i+1; j<${hostname_len}; j++ ))
do
wg genpsk > ${configdir}/${meshnetwork}_presharedkey_${hostnames[i]}_${hostnames[j]}
cat << EOF >> ${configdir}/${meshnetwork}_${hostnames[i]}.conf
# ${meshnetwork} ${hostnames[j]} ${wg_subnet[${meshnetwork}]}${hosts[${hostnames[j]}]}
[Peer]
PublicKey = `cat ${configdir}/${meshnetwork}_public_${hostnames[j]}`
AllowedIPs = ${wg_subnet[${meshnetwork}]}${hosts[${hostnames[j]}]}
Endpoint = `dig ${hostnames[j]} +short`:${wg_port[${meshnetwork}]}
PresharedKey = `cat ${configdir}/${meshnetwork}_presharedkey_${hostnames[i]}_${hostnames[j]}`
PersistentKeepalive = 25
EOF
cat << EOF >> ${configdir}/${meshnetwork}_${hostnames[j]}.conf
# ${meshnetwork} ${hostnames[i]} ${wg_subnet[${meshnetwork}]}${hosts[${hostnames[i]}]}
[Peer]
PublicKey = `cat ${configdir}/${meshnetwork}_public_${hostnames[i]}`
AllowedIPs = ${wg_subnet[${meshnetwork}]}${hosts[${hostnames[i]}]}
Endpoint = `dig ${hostnames[i]} +short`:${wg_port[${meshnetwork}]}
PresharedKey = `cat ${configdir}/${meshnetwork}_presharedkey_${hostnames[i]}_${hostnames[j]}`
PersistentKeepalive = 25
EOF
done
done
for host in ${!hosts[@]}
do
cat << EOF >> ${configdir}/interface_${meshnetwork}_${host}.conf
auto ${meshnetwork}
iface ${meshnetwork} inet static
address ${wg_subnet[${meshnetwork}]}${hosts[${host}]}/24
pre-up ip link add dev ${meshnetwork} type wireguard
pre-up wg setconf ${meshnetwork} /etc/wireguard/${meshnetwork}.conf
post-down ip link del ${meshnetwork}
mtu 1280
EOF
done
chmod 0600 *
for host in ${!hosts[@]}
do
scp -o StrictHostKeyChecking=accept-new -p -P ${sshport} ${configdir}/${meshnetwork}_${host}.conf root@${host}:/etc/wireguard/${meshnetwork}.conf
scp -o StrictHostKeyChecking=accept-new -p -P ${sshport} ${configdir}/interface_${meshnetwork}_${host}.conf root@${host}:/tmp/${meshnetwork}.conf
ssh -o StrictHostKeyChecking=accept-new root@${host} "cat /tmp/${meshnetwork}.conf >> /etc/network/interfaces"
done
done
rm -rf ${configdir}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment