Skip to content

Instantly share code, notes, and snippets.

@dotsh
Created April 15, 2021 16:01
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 dotsh/a26bf0e143ae3fdd6371007347812153 to your computer and use it in GitHub Desktop.
Save dotsh/a26bf0e143ae3fdd6371007347812153 to your computer and use it in GitHub Desktop.
Wireguard - script to add new client
#! /bin/bash
clientname=$1
serverconf="wg0.conf"
subnet24="172.20.1."
dns1=176.103.130.130
dns2=176.103.130.131
newprivkey=$(wg genkey)
newpubkey=$(echo $newprivkey | wg pubkey)
lastdigit=$(echo $(cat $serverconf | grep -oP "(?<=AllowedIPs = $subnet24)[^/]*" | sort -n | tail -1) + 1 | bc )
serverpubkey=$(cat $serverconf | grep -P "^\s*PrivateKey" | sed 's/^\s*PrivateKey\s*=\s*\(.*\)$/\1/' | wg pubkey)
listenport=$(cat $serverconf | grep -P "^\s*ListenPort" | sed 's/^\s*ListenPort\s*=\s*\(.*\)$/\1/' )
listenfqdn=$(nslookup $(curl -s ifconfig.me) | grep -oP "(?<=name = ).*(?=[.])")
cat > $clientname.conf << EOF
[Interface]
PrivateKey = ${newprivkey}
Address = ${subnet24}${lastdigit}/24
DNS = ${dns1},${dns2}
[Peer]
PublicKey = $serverpubkey
Endpoint = ${listenfqdn}:${listenport}
AllowedIPs = 0.0.0.0/0
EOF
cat >> $serverconf << EOF
#$clientname
[Peer]
publicKey = $newpubkey
AllowedIPs = ${subnet24}${lastdigit}/32
EOF
wg syncconf wg0 <(wg-quick strip wg0)
/usr/bin/qrencode -t ansiutf8 < $clientname.conf
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment