Skip to content

Instantly share code, notes, and snippets.

@cweiland
Last active January 4, 2025 01:24
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=("net1" "net2")
declare -A hosts=(
[hostname1]=1
[hostname2]=2
[hostname3]=3
)
declare -A wg_port=(
[net1]=1655
[net2]=1656
)
declare -A wg_subnet=(
[net1]=10.10.10.
[net2]=10.10.11.
)
declare -A keys
declare -A ips
configdir="/tmp/wireguard"
sshport=21
# Création du dossier de configuration
mkdir -p "${configdir}" || { echo "Échec de la création de ${configdir}"; exit 1; }
hostnames=("${!hosts[@]}")
hostname_len=${#hostnames[@]}
for meshnetwork in "${meshnetworks[@]}"; do
for hostname in "${hostnames[@]}"; do
# Obtenir les IP et générer les clés
ips[${hostname}_public]=$(getent hosts "${hostname}" | awk '{print $1}')
ips[${hostname}_${meshnetwork}]="${wg_subnet[${meshnetwork}]}${hosts[${hostname}]}"
keys[${hostname}_private]=$(wg genkey)
keys[${hostname}_public]=$(echo "${keys["${hostname}_private"]}" | wg pubkey)
# Fichier de configuration WireGuard
cat << EOF > "${configdir}/${meshnetwork}_${hostname}.conf"
# ${meshnetwork} ${hostname} ${ips[${hostname}_${meshnetwork}]}
[Interface]
PrivateKey = ${keys[${hostname}_private]}
ListenPort = ${wg_port[${meshnetwork}]}
EOF
done
for ((i = 0; i < hostname_len; i++)); do
for ((j = i + 1; j < hostname_len; j++)); do
# Génération des clés pré-partagées
keys["${hostnames[i]}_${hostnames[j]}_psk"]=$(wg genpsk)
cat << EOF >> "${configdir}/${meshnetwork}_${hostnames[i]}.conf"
# ${meshnetwork} ${hostnames[j]} ${ips[${hostnames[j]}_${meshnetwork}]}
[Peer]
PublicKey = ${keys[${hostnames[j]}_public]}
AllowedIPs = ${ips[${hostnames[j]}_${meshnetwork}]}/32
Endpoint = ${ips[${hostnames[j]}_public]}
PresharedKey = ${keys[${hostnames[i]}_${hostnames[j]}_psk]}
PersistentKeepalive = 25
EOF
cat << EOF >> "${configdir}/${meshnetwork}_${hostnames[j]}.conf"
# ${meshnetwork} ${hostnames[i]} ${ips[${hostnames[i]}_${meshnetwork}]}
[Peer]
PublicKey = ${keys[${hostnames[i]}_public]}
AllowedIPs = ${ips[${hostnames[i]}_${meshnetwork}]}/32
Endpoint = ${ips[${hostnames[i]}_public]}
PresharedKey = ${keys[${hostnames[i]}_${hostnames[j]}_psk]}
PersistentKeepalive = 25
EOF
done
done
for hostname in "${!hosts[@]}"; do
# Fichier d'interface réseau
cat << EOF > "${configdir}/interface_${meshnetwork}_${hostname}.conf"
auto ${meshnetwork}
iface ${meshnetwork} inet static
address ${ips[${hostname}_${meshnetwork}]}/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 "${configdir}"/*
#for host in "${!hosts[@]}"; do
# Transfert des fichiers et mise à jour de la configuration
#scp -o StrictHostKeyChecking=accept-new -P "${sshport}" \
# "${configdir}/${meshnetwork}_${host}.conf" "root@${ips[${hostname}_public]}:/etc/wireguard/${meshnetwork}.conf"
#scp -o StrictHostKeyChecking=accept-new -P "${sshport}" \
# "${configdir}/interface_${meshnetwork}_${host}.conf" "root@${ips[${hostname}_public]}:/tmp/${meshnetwork}.conf"
#
#ssh -o StrictHostKeyChecking=accept-new -p "${sshport}" \
# "root@${ips[${hostname}_public]}" "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