Skip to content
Notifications
Clear all

Showcase: My script to auto-expand IP ranges in address groups from a CSV.

2 Posts
2 Users
0 Reactions
0 Views
(@calebw)
Trusted Member
Joined: 2 weeks ago
Posts: 58
Topic starter   [#23641]

Alright, let's talk about one of the more tedious parts of managing a FortiGate at any kind of scale: address objects and groups. You've got a lovely spreadsheet from some other team—probably networking, maybe security ops—with a list of IP ranges that need to go into a policy. Manually converting each /24 or /22 into individual objects feels like the kind of busywork we invented computers to avoid. Yet, here we are, clicking through the GUI like it's 2005.

I got tired of it. So, I did what any pragmatic but automation-obsessed person would do: I wrote a script to eat a CSV and spit out the necessary CLI commands. It's not sentient AI, but it gets the job done without the existential dread of manual entry. The core idea is simple: parse the CSV, expand the CIDR ranges into individual IPs (or subnets, if you prefer), generate unique object names, and bundle them into an address group. The real "value" is in handling the inevitable formatting quirks and keeping everything traceable.

Here's the general workflow the script follows:

* **Input:** A CSV file with at least a column for the base name and a column for the CIDR range (e.g., `Trusted_Servers, 10.0.4.0/24`).
* **Expansion:** It uses a simple IP network library (Python's `ipaddress` is your friend here) to explode that `/24` into 256 individual `/32` host objects. You can configure it to create smaller subnets if you hate having that many objects, but I find the granularity useful for logging.
* **Naming Convention:** It creates objects with a predictable pattern: `{base_name}_{sequential_number}`. This makes it clear what group they belong to and prevents naming collisions.
* **Output:** It generates a clean text file with the exact `config firewall address` and `config firewall addrgrp` CLI commands. You can then review them, make any adjustments, and paste them directly into your FortiGate's CLI.

The main pitfalls I've run into are mostly around scale and sanity. Creating thousands of objects will make your config file larger, and the GUI will understandably groan when you try to view the address group. But for policy application and processing, the FortiGate handles it just fine. Also, double-check your CSV for any typos or overlapping ranges before you run the script—it's very literal and will happily create duplicate objects if you tell it to.

It's a blunt instrument, but it saves hours. I'm sure there are fancy commercial tools or Ansible modules that do this with more bells and whistles, but sometimes you just need a quick, dirty, and controlled solution that doesn't require another license or a PhD in YAML. If you've built something similar, or have a better method for wrangling bulk address objects, I'm all ears. Always looking to refine the process.

– Caleb


It's just pattern matching


   
Quote
(@danm)
Reputable Member
Joined: 3 weeks ago
Posts: 199
 

Completely agree about the spreadsheet-to-CLI grind. I've done something similar, but I'd add a word of caution: watch out for huge range expansions. I once let a script loose on a /16 and nearly crashed our config parser with thousands of single-IP objects. Sometimes it's better to keep the subnet as a single object if the policy logic allows it. What's your cutoff for deciding to expand vs. keep as CIDR?



   
ReplyQuote