Created
October 5, 2023 09:56
-
-
Save eru123/d92881359da95ae9b3630e65690bf4ba to your computer and use it in GitHub Desktop.
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
cfuser=<Your Cloudflare Account Email Address> | |
cftoken=<Cloudflare Global API KEY> | |
# get arg | |
arg1=$1 | |
arg2=$2 | |
# if $arg1 is not equal to unban, and arg1 is not empty | |
if [ "$arg1" != "unban" ] && [ -n "$arg1" ]; then | |
# get ip | |
ip=$arg1 | |
# if $arg2 is not empty | |
if [ -n "$arg2" ]; then | |
# get note | |
note=$arg2 | |
else | |
# set note | |
note="CfBan" | |
# ban ip | |
echo "Banning $ip" | |
curl -s -X POST "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules" \ | |
-H "X-Auth-Email: $cfuser" \ | |
-H "X-Auth-Key: $cftoken" \ | |
-H "Content-Type: application/json" \ | |
--data "{\"mode\":\"block\",\"configuration\":{\"target\":\"ip\",\"value\":\"$ip\"},\"notes\":\"$note\"}" | |
exit 0 | |
fi | |
fi | |
if [ "$arg1" == "unban" ] && [ -n "$arg2" ]; then | |
# get ip | |
ip=$arg2 | |
# unban ip | |
echo "Unbanning $ip" | |
curl -s -X DELETE "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules?mode=block&configuration_target=ip&configuration_value=$ip" \ | |
-H "X-Auth-Email: $cfuser" \ | |
-H "X-Auth-Key: $cftoken" \ | |
-H "Content-Type: application/json" | |
exit 0 | |
fi | |
# USAGE | |
echo "$0" | |
echo "Usage: $0 <ip> [note]" | |
echo "Usage: $0 unban <ip>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment