Last active
August 1, 2025 15:44
-
-
Save selfagency/4a4c2aa4340323ed56f74ef57f8bde1c to your computer and use it in GitHub Desktop.
update digital ocean firewall
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
# Requires doctl and the "public-ip-cli" npm module to be globally installed. | |
#!/bin/sh | |
FWID="YOUR_FIREWALL_ID" | |
IPV4=$("${HOME}/.nodebrew/current/bin/public-ip" --4) | |
#IPV6=$("${HOME}/.nodebrew/current/bin/public-ip" --6) | |
printf "🔥 Updating DO firewall rules\n\n" | |
OLD_RULES="$(doctl compute firewall get "${FWID}" --format InboundRules | tr ' ' '\n' | grep -E 'ports:22|ports:2022' | tr '\n' ' ' | sed '$ s/.$//')" | |
if [ "${OLD_RULES}" = "\"\"" ]; then | |
printf "No rules to remove.\n\n" | |
else | |
printf "Removing %s\n\n" "${OLD_RULES}" | |
REMOVE="doctl compute firewall remove-rules ${FWID} --inbound-rules=${OLD_RULES} -v" | |
# echo "${REMOVE}" | |
eval "${REMOVE}" | |
fi | |
# NEW_RULES="\"protocol:tcp,ports:22,address:${IPV4},address:${IPV6} | |
# protocol:udp,ports:60000-61000,address:${IPV4},address:${IPV6}\"" | |
NEW_RULES="\"protocol:tcp,ports:22,address:${IPV4} protocol:udp,ports:60000-61000,address:${IPV4}\"" | |
printf "Adding %s\n\n" "${NEW_RULES}" | |
ADD="doctl compute firewall add-rules ${FWID} --inbound-rules=${NEW_RULES} -v" | |
# echo "${ADD}" | |
eval "${ADD}" | |
printf "🏁 Done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment