Skip to content

Instantly share code, notes, and snippets.

@jezman
Last active April 24, 2025 11:57
Show Gist options
  • Save jezman/34cd5c399180f4049155c90f1811f2a8 to your computer and use it in GitHub Desktop.
Save jezman/34cd5c399180f4049155c90f1811f2a8 to your computer and use it in GitHub Desktop.
Simple create peers for wireguard.
#!/bin/bash
TMPDIR=/tmp/wg_peers
read -p "Username: " USERNAME
USER_DIR=$TMPDIR/$USERNAME
USER_PRIVATEKEY=$USER_DIR/$USERNAME
USER_PUBLICKEY=$USER_DIR/$USERNAME.pub
USER_CONF=$USER_DIR/$USERNAME.conf
USER_QR=$USER_DIR/qrcode.png
USER_MIKROTIK=$USER_DIR/mikrotik.txt
mkdir -p $USER_DIR
# Generate keys for user
wg genkey | tee $USER_PRIVATEKEY | wg pubkey > $USER_PUBLICKEY
# Read data from input
read -p "Server publickey: " SERVER_PUBLICKEY
read -p "Server endpoint(123.123.123.123:4321): " ENDPOINT
read -p "Peer ip(example: 192.168.5.5/32): " PEER_IP
read -p "Peer allowed ips(example: 0.0.0.0/0): " ALLOWED_IPS
# Generate wireguard conf file
echo "# $USERNAME
[Interface]
PrivateKey = $(cat $USER_PRIVATEKEY)
Address = $PEER_IP
DNS = 8.8.8.8
MTU = 1280
[Peer]
PublicKey = $SERVER_PUBLICKEY
AllowedIPs = $ALLOWED_IPS
Endpoint = $ENDPOINT
PersistentKeepalive = 15" > $USER_CONF
# Generate qr code and save to file
qrencode -t ansiutf8 < $USER_CONF
qrencode -t png -o $USER_QR -r $USER_CONF
# Mikrotik command
echo "/interface/wireguard/peers/add name=$USERNAME interface=wireguard1 public-key=$(cat $USER_PUBLICKEY) allowed-address=0.0.0.0/0 persistent-keepalive=15" > $USER_MIKROTIK
echo "Add this line in mikrotik cli:"
cat $USER_MIKROTIK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment