Skip to content

Instantly share code, notes, and snippets.

@iarp
Last active October 26, 2021 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iarp/ed44636defd35e73f87535117c77be33 to your computer and use it in GitHub Desktop.
Save iarp/ed44636defd35e73f87535117c77be33 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Need a name for the peer"
exit 1;
fi
GEN_FILE_SAVE_LOCATION="/etc/wireguard/peer_files"
PUBLIC_IP="123.456.789.111:51820"
INTERNAL_IP_RANGE="10.0.8.0/24"
INTERNAL_IP_BASE="10.0.8"
mkdir -p $GEN_FILE_SAVE_LOCATION
LAST_IP=$(< /etc/wireguard/last_assigned_ip.txt)
SERVER_PUB_KEY=$(< /etc/wireguard/public.key)
PRI_KEY=$(wg genkey)
PUB_KEY=$(echo "$PRI_KEY" | wg pubkey)
NEXT_IP=$((LAST_IP+1))
OUTPUT_FILE="$GEN_FILE_SAVE_LOCATIONS/$NEXT_IP-$1.conf"
echo $OUTPUT_FILE
echo "[Interface]" > $OUTPUT_FILE
echo "# $NEXT_IP-$1.conf" >> $OUTPUT_FILE
echo "PrivateKey = $PRI_KEY" >> $OUTPUT_FILE
echo "Address = $INTERNAL_IP_BASE.$NEXT_IP/32" >> $OUTPUT_FILE
echo "[Peer]" >> $OUTPUT_FILE
echo "PublicKey = $SERVER_PUB_KEY" >> $OUTPUT_FILE
echo "AllowedIPs = $INTERNAL_IP_RANGE" >> $OUTPUT_FILE
echo "Endpoint = $PUBLIC_IP" >> $OUTPUT_FILE
wg set wg0 peer $PUB_KEY allowed-ips $INTERNAL_IP_BASE.$NEXT_IP
echo $NEXT_IP > /etc/wireguard/last_assigned_ip.txt
cat $OUTPUT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment