Fast and efficient CLI tool for managing IPv4/IPv6 CIDR ranges. Built with Go and optimized for use in GitHub Actions.
- cidrmgr
- β‘ Fast: Efficient CIDR range merging using the
seancfoley/ipaddress-golibrary - π Flexible I/O: Read from files or stdin, write to files or stdout
- π IPv4 & IPv6 Support: Handles both IPv4 and IPv6 CIDR ranges
- π§Ή Clean Output: Automatically deduplicates and sorts ranges
- π Comments Support: Ignore lines starting with
# - π CI/CD Ready: Perfect for GitHub Actions workflows
git clone https://github.com/viktor45/cidrmgr.git
cd cidrmgr
go build -o cidrmgrCreate a Makefile for easier building:
.PHONY: build test clean
build:
go build -o cidrmgr
test:
go test -v ./...
test-coverage:
go test -coverage ./...
clean:
rm -f cidrmgr
go clean -testcacheThen run:
make build
make test# Show help
cidrmgr help
cidrmgr -h
# Show version
cidrmgr version
cidrmgr -vThe merge command combines overlapping IPv4/IPv6 CIDR ranges and removes duplicates.
cidrmgr merge [options]-i string- Input file path (reads from stdin if not specified)-o string- Output file path (writes to stdout if not specified)
Merge from file to file:
cidrmgr merge -i ranges.txt -o merged.txtMerge from file to stdout:
cidrmgr merge -i ranges.txtMerge from stdin to file:
cat ranges.txt | cidrmgr merge -o merged.txtPipe input and output:
cat ranges.txt | cidrmgr merge > merged.txtDefault stdin/stdout:
cidrmgr merge
# (reads from stdin, writes to stdout)Each line should contain a single CIDR range:
- IPv4:
192.168.0.0/24 - IPv6:
2001:db8::/32
Lines starting with # are treated as comments and ignored.
Empty lines are also ignored.
# Private IPv4 ranges
192.168.0.0/24
192.168.1.0/24
192.168.0.128/25
# RFC1918 ranges
10.0.0.0/8
172.16.0.0/12
# IPv6 ranges
2001:db8::/32
fc00::/7
# Duplicate (will be removed)
192.168.0.0/24
10.0.0.0/8
172.16.0.0/12
192.168.0.0/23
2001:db8::/32
fc00::/7
Use cidrmgr in your GitHub Actions workflows:
name: Merge CIDR Ranges
on: [push]
jobs:
merge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.25'
- name: Build cidrmgr
run: go build -o cidrmgr
- name: Merge CIDR ranges
run: ./cidrmgr merge -i allowlist.txt -o merged.txt
- name: Upload merged ranges
uses: actions/upload-artifact@v3
with:
name: merged-ranges
path: merged.txtThe tool is optimized for performance and can handle large files efficiently:
- Processes thousands of CIDR ranges quickly
- Memory efficient with streaming input/output
- Perfect for CI/CD pipelines with strict time limits
Run benchmarks:
go test -bench=. -benchmem ./...Run the comprehensive test suite:
# Run all tests
go test -v ./...
# Run specific test
go test -v -run TestMergeIPv4Basic ./...
# With coverage
go test -v -cover ./...
# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.outThe project includes comprehensive tests for:
- β Basic IPv4 range merging
- β IPv6 range merging
- β Mixed IPv4/IPv6 handling
- β Duplicate removal
- β Comment and empty line handling
- β Stdin/stdout operations
- β File I/O
- β Invalid input handling
- β Performance benchmarks
- Go 1.25 or later
github.com/seancfoley/ipaddress-go- IP address manipulation library
The tool provides clear error messages for common issues:
# Invalid CIDR format
$ cidrmgr merge -i bad.txt
Error: invalid CIDR format '192.168.0.256/24': ...
# File not found
$ cidrmgr merge -i nonexistent.txt
Error: failed to read input: open nonexistent.txt: no such file or directory
# Missing input file permissions
$ cidrmgr merge -i restricted.txt
Error: failed to read input: permission denied- Sorted: Output is alphabetically sorted
- Deduplicated: Duplicate ranges are removed
- Optimized: Overlapping ranges are merged into larger blocks
- One per line: Each range is on its own line
- No trailing whitespace: Clean output format
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - see LICENSE file for details
- Built with seancfoley/ipaddress-go library
- Optimized for GitHub Actions and CI/CD pipelines
Make sure your CIDR ranges are properly formatted:
- IPv4:
10.0.0.0/8(not10.0.0.0/8.0) - IPv6:
2001:db8::/32(not2001:db8)
Verify your system has sufficient RAM and try:
- Breaking the file into smaller chunks
- Using a faster disk (SSD preferred)
- Running on a machine with more CPU cores
If processing very large files:
- Increase available memory
- Process the file in chunks
- Use a machine with more RAM
# 1. Clone and build
git clone https://github.com/viktor45/cidrmgr.git
cd cidrmgr
go build -o cidrmgr
# 2. Create a test file
cat > test.txt << EOF
192.168.0.0/24
192.168.1.0/24
10.0.0.0/8
2001:db8::/32
EOF
# 3. Merge ranges
./cidrmgr merge -i test.txt -o output.txt
# 4. View results
cat output.txtMade with β€οΈ for the DevOps community