Created
December 8, 2020 17:52
-
-
Save bilalatli/509f026e889959682404e6aefc720c3d to your computer and use it in GitHub Desktop.
Wireguard VPN Client Create Shell Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| if [ -z "$1" ] | |
| then | |
| echo "Client name not defined" | |
| exit 0 | |
| fi | |
| CLIENT_NAME=$1 | |
| cd /etc/wireguard/clients | |
| mkdir "$CLIENT_NAME" | |
| cd "$CLIENT_NAME" | |
| wg genkey | tee "$CLIENT_NAME".private | wg pubkey > "$CLIENT_NAME".public | |
| echo "Done!" | |
| exit 0 | |
| SERVER_CONF=/etc/wireguard/wg0.conf | |
| SERVER_PUBLIC=$(cat /etc/wireguard/server.public) | |
| CLIENT_PUBLIC=$(cat "$CLIENT_NAME".public) | |
| CLIENT_PRIVATE=$(cat "$CLIENT_NAME".private) | |
| CLIENT_IP=$(cat "$SERVER_CONF" | grep "# CLIENT-" | grep -v grep | wc -l) | |
| CLIENT_IP=$(($CLIENT_IP + 100 + 1)) | |
| echo "# CLIENT-${CLIENT_IP}" >> $SERVER_CONF | |
| echo "[Peer]" >> $SERVER_CONF | |
| echo "PublicKey = ${CLIENT_PUBLIC}" >> $SERVER_CONF | |
| echo "AllowedIPs = 10.200.200.${CLIENT_IP}" >> $SERVER_CONF | |
| echo "" >> $SERVER_CONF | |
| SERVER_IP=$(curl -s -4 https://icanhazip.com/) | |
| (cat <<EOF > "$CLIENT_NAME.conf" | |
| # ${CLIENT_NAME} | |
| [Interface] | |
| PrivateKey = ${CLIENT_PRIVATE} | |
| Address = 10.200.200.${CLIENT_IP}/32 | |
| DNS = 1.1.1.1, 8.8.8.8 | |
| [Peer] | |
| PublicKey = ${SERVER_PUBLIC} | |
| AllowedIPs = 0.0.0.0/0 | |
| Endpoint = $SERVER_IP:51820 | |
| PersistentKeepalive = 25 | |
| EOF | |
| ) | |
| # qrencode -t ansiutf8 < "$CLIENT_NAME.conf" | |
| qrencode -t png -o "$CLIENT_NAME.png" < "$CLIENT_NAME.conf" | |
| systemctl restart [email protected] | |
| echo "SUCCESS: ${CLIENT_NAME} local ip address : 10.200.200.${CLIENT_IP}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment