Last active
December 20, 2024 15:21
-
-
Save miguelangel-nubla/137fea6653a3d612ca66cd7f8ea7edb6 to your computer and use it in GitHub Desktop.
Mikrotik script to generate address lists from URLs
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
# Function to update an address list from multiple URLs | |
:local updateAddressList do={ | |
:do { | |
# Clear existing entries if not updated | |
/ip firewall address-list set [find list=$listName dynamic=no] timeout=$timeout dynamic=yes | |
/ipv6 firewall address-list set [find list=$listName dynamic=no] timeout=$timeout dynamic=yes | |
# Loop through each URL | |
:foreach url in=$urls do={ | |
# Fetch data from the current URL | |
:local data ([:tool fetch url=$url output=user as-value]->"data") | |
:local listEnd false | |
# Process each line in the fetched data | |
:while ([:len $data] != 0 && !$listEnd) do={ | |
:local endPos [:find $data "\n"] | |
# Determine if this is the last line | |
:if ($endPos < 0) do={ | |
:set listEnd true | |
:set endPos [:len $data] | |
} | |
# Extract the IP address from the current line | |
:local ip [:pick $data 0 $endPos] | |
# Go to IPv4 list | |
/ip firewall address-list | |
# Validate that the line contains an IPv4 address | |
:if ($ip ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}") do={ | |
:do { | |
# Try to add the IP to the address list | |
add list=$listName address=$ip comment=$description timeout=0 dynamic=no | |
} on-error={ | |
# Update the existing entry if it already exists | |
set [find list=$listName address=$ip] comment=$description timeout=0 dynamic=no | |
} | |
} | |
# Go to IPv6 list | |
/ipv6 firewall address-list | |
# Validate that the line contains an IPv6 address | |
:if ($ip~"^([a-f0-9:]+:+)+[a-f0-9]+") do={ | |
:do { | |
# Try to add the IP to the address list | |
add list=$listName address=$ip comment=$description timeout=0 dynamic=no | |
} on-error={ | |
# Update the existing entry if it already exists | |
set [find list=$listName address=$ip] comment=$description timeout=0 dynamic=no | |
} | |
} | |
# Remove the processed line from the data | |
:set data [:pick $data ($endPos + 1) [:len $data]] | |
} | |
} | |
} on-error={ | |
:log warning "Failed to update address list <$listName> from <$url>" | |
} | |
} | |
# Call the function to update the Cloudflare address lists | |
:local urls {"https://www.cloudflare.com/ips-v4"; "https://www.cloudflare.com/ips-v6"} | |
$updateAddressList urls=$urls listName="CLOUDFLARE" timeout=1m description="Cloudflare IPs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment