Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created May 17, 2024 08:20
Show Gist options
  • Save HirbodBehnam/c51c788afa70311734fdd68d34d50033 to your computer and use it in GitHub Desktop.
Save HirbodBehnam/c51c788afa70311734fdd68d34d50033 to your computer and use it in GitHub Desktop.
Rotate your server's IPv6
#!/bin/bash
# Put it in crontab: */5 * * * * /bin/bash /root/scripts/ipv6-rotate.sh
# Get the next IPv6
CURRENT_IP=$(cat ~/.config/rotate-ip)
NEXT_IP=$(((CURRENT_IP+1)%65534))
echo "$NEXT_IP" > ~/.config/rotate-ip
CURRENT_IP=$(printf "%x" "$CURRENT_IP")
NEXT_IP=$(printf "%x" "$NEXT_IP")
CURRENT_IP="x:x:x::$CURRENT_IP"
NEXT_IP="x:x:x::$NEXT_IP"
echo "$CURRENT_IP $NEXT_IP"
# Change the IPv6 in cloudflare
curl --request PATCH \
--url https://api.cloudflare.com/client/v4/zones/123456/dns_records/123456 \
-H "Authorization: Bearer 123456" \
-H "Content-Type:application/json" \
--data "{\"content\":\"$NEXT_IP\",\"name\":\"www.yourdomain.com\",\"type\":\"AAAA\"}"
# Add the IPv6 to the server and remove the old one
ip address del "$CURRENT_IP/64" dev eth0
ip address add "$NEXT_IP/64" dev eth0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment