How to add bulk IP addresses to DigitalOcean firewall ?

digitalocean

Trying to add bulk IP’s in DigitalOcean firewall? You are in the right place.

DigitalOcean cloud control panel UI doesn’t allow you to paste in multiple IP Addresses at once. That’s a good idea for a UI improvement.

In the meantime you can definitely do it via the API

Use the following shell script

It uses json for POST data, so update TOKEN, FIREWALL NAME, IP addresses, ports… and then run the script

#!/bin/bash

# Author: Akhil Jalagam
# update TOKEN, FIREWALL NAME, IP addresses and then run the script

TOKEN=dfjvbidvbasb4l5tu45hvu46vgl45h6vl
FIREWALL_NAME=internalaccess
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d \
'{
  "name": "'$FIREWALL_NAME'",
  "inbound_rules": [
    {
      "protocol": "tcp",
      "ports": "all",
      "sources": {
        "addresses": [
          "196.112.47.128/29",
          "196.112.43.128/29"
        ]
      }
    },
    {
      "protocol": "udp",
      "ports": "all",
      "sources": {
        "addresses": [
          "196.112.47.128/29",
          "196.112.43.128/29"

        ]
      }
    },
    {
      "protocol": "icmp",
      "sources": {
        "addresses": [
          "0.0.0.0/0",
          "::/0"
        ]
      }
    }
  ],
  "outbound_rules": [
    {
      "protocol": "icmp",
      "destinations": {
        "addresses": [
          "0.0.0.0/0",
          "::/0"
        ]
      }
    },
    {
      "protocol": "tcp",
      "ports": "all",
      "destinations": {
        "addresses": [
          "0.0.0.0/0",
          "::/0"
        ]
      }
    },
    {
      "protocol": "udp",
      "ports": "all",
      "destinations": {
        "addresses": [
          "0.0.0.0/0",
          "::/0"
        ]
      }
    }
  ]
}' "https://api.digitalocean.com/v2/firewalls/"

Hope it will save your time ! 😊