Created
May 13, 2016 13:17
-
-
Save pjv/926ece8549cd45bac4821945f6ad253c to your computer and use it in GitHub Desktop.
Cloudflare v4 API ban and urban bash scripts
This file contains 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
#!/bin/bash | |
# dependencies: curl | |
# example usage: cf_ban 192.168.0.1 | |
# append cloudflare email address and API token: | |
USER= | |
TOKEN= | |
curl -sSX POST "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules" \ | |
-H "X-Auth-Email: $USER" \ | |
-H "X-Auth-Key: $TOKEN" \ | |
-H "Content-Type: application/json" \ | |
--data "{\"mode\":\"block\",\"configuration\":{\"target\":\"ip\",\"value\":\"$1\"},\"notes\":\"Blocked via cf_ban script\"}" |
This file contains 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
#!/bin/bash | |
# dependencies: curl, jq (https://stedolan.github.io/jq/) | |
# example usage: cf_unban 192.168.0.1 | |
# append cloudflare email address and API token: | |
USER= | |
TOKEN= | |
# get the rule ID | |
JSON=$(curl -sSX GET "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules?mode=block&configuration_target=ip&configuration_value=$1" \ | |
-H "X-Auth-Email: $USER" \ | |
-H "X-Auth-Key: $TOKEN" \ | |
-H "Content-Type: application/json") | |
ID=$(echo $JSON | jq -r '.result[].id') | |
# unban IP by deleting the rule | |
curl -sSX DELETE "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules/$ID" \ | |
-H "X-Auth-Email: $USER" \ | |
-H "X-Auth-Key: $TOKEN" \ | |
-H "Content-Type: application/json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do a lot more than this with the v4 API. These little scripts are to minimally re-create ban and unban functionality from the v1 API.