Skip to content

Instantly share code, notes, and snippets.

@stream2me
Forked from craigmdennis/update.sh
Last active October 25, 2024 04:08
Show Gist options
  • Select an option

  • Save stream2me/7e825e7ff21714e8023a847944118475 to your computer and use it in GitHub Desktop.

Select an option

Save stream2me/7e825e7ff21714e8023a847944118475 to your computer and use it in GitHub Desktop.
Update Cloudflare Access Group IP address based on current public IP address from the requestor
ACCOUNT_ID="YOUR_ACCOUNT_ID"
GROUP_ID="YOUR_ACCESS_GROUP_ID"
GROUP_NAME="YOUR_ACCESS_GROUP_NAME"
API_TOKEN="YOUR_CLOUDFLARE_API_KEY"
IP_ADDRESS=$(curl -s https://api.ipify.org)
OLD_IP=$(curl -s \
--request GET \
--url https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/groups/$GROUP_ID \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $API_TOKEN" \
| grep '"ip": "' | cut -b18-31)
if [ "$OLD_IP" = "$IP_ADDRESS" ]; then
echo "Address is still: $OLD_IP"
else
echo "IP changed..."
curl -s -o /dev/null \
--request PUT \
--url https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/groups/$GROUP_ID \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $API_TOKEN" \
--data '{
"include": [
{
"ip": {
"ip": "'"$IP_ADDRESS"'"
}
}
],
"name": "'"$GROUP_NAME"'"
}'
echo "Updated $GROUP_NAME to include $IP_ADDRESS"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment