Prefix Fetcher is a Python-based tool for fetching country-specific IP prefixes from BGP data. Dynamically fetches ASN lists from Regional Internet Registry (RIR) delegated files and filters BGP prefixes accordingly.
- Dynamic ASN Fetching: Automatically downloads the latest ASN allocations from RIR delegated files
- Iran (IR): Uses RIPE NCC delegated data
- China (CN): Uses APNIC delegated data
- Russia (RU): Uses RIPE NCC delegated data
- Comprehensive Coverage: Fetches from all RIRs for complete ASN discovery
- BGP Data: Fetches IP prefixes from bgp.tools
- Multiple Output Formats:
- Regular form: Original BGP prefixes as-is
- Collapsed form: Shortest aggregated prefixes (automatically generated)
- Split form: IPv4 as /24 blocks, IPv6 as /48 blocks (optional with
--splitflag)
- Clean & Simple: No caching, fresh data on every run
- Automatic retry logic with exponential backoff
- Duplicate removal: Automatically deduplicates ASNs found across multiple RIRs
- Python (>=3.14)
- uv (recommended) or pip for package management
-
Clone the repository:
git clone https://github.com/compassvpn/prefix-fetcher.git cd prefix-fetcher -
Install dependencies:
Using uv (recommended):
uv sync
Using pip:
pip install -e .
Using uv:
uv run python main.py [OPTIONS]Or after installation:
python main.py [OPTIONS]| Option | Description |
|---|---|
--fetch-ir |
Fetch Iranian IP prefixes |
--fetch-cn |
Fetch Chinese IP prefixes |
--fetch-ru |
Fetch Russian IP prefixes |
--split |
Also generate split form files (IPv4 as /24, IPv6 as /48) |
-h, --help |
Show help information |
-
Fetch Iranian IP prefixes (regular and collapsed forms):
uv run python main.py --fetch-ir
-
Fetch Chinese IP prefixes with split form:
uv run python main.py --fetch-cn --split
-
Fetch Russian IP prefixes:
uv run python main.py --fetch-ru
-
Fetch multiple countries with split form:
uv run python main.py --fetch-ir --fetch-cn --fetch-ru --split
-
Show help:
uv run python main.py --help
The tool generates country-specific prefix files in multiple formats:
Regular form (original BGP prefixes):
ir_prefixes_v4.txt/ir_prefixes_v6.txtcn_prefixes_v4.txt/cn_prefixes_v6.txtru_prefixes_v4.txt/ru_prefixes_v6.txt
Collapsed form (shortest aggregated prefixes):
ir_prefixes_v4_collapsed.txt/ir_prefixes_v6_collapsed.txtcn_prefixes_v4_collapsed.txt/cn_prefixes_v6_collapsed.txtru_prefixes_v4_collapsed.txt/ru_prefixes_v6_collapsed.txt
Split form (IPv4 as /24 blocks, IPv6 as /48 blocks):
ir_prefixes_v4_split.txt/ir_prefixes_v6_split.txtcn_prefixes_v4_split.txt/cn_prefixes_v6_split.txtru_prefixes_v4_split.txt/ru_prefixes_v6_split.txt
- Regular: Original BGP prefixes as announced (varying prefix lengths)
- Collapsed: Optimized/aggregated prefixes merged into shortest possible form
- Split: Normalized to fixed-size blocks (/24 for IPv4, /48 for IPv6)
- Fetches ASN Lists: Downloads the latest ASN allocations from all RIRs for comprehensive coverage:
- Iran: Primary coverage from RIPE NCC, with additional checks across all RIRs
- China: Primary coverage from APNIC, with additional checks across all RIRs
- Russia: Primary coverage from RIPE NCC, with additional checks across all RIRs
- Downloads BGP Data: Fetches the complete BGP routing table from bgp.tools
- Filters by ASN: Keeps only prefixes from the dynamically fetched ASN lists
- Generates Multiple Formats:
- Regular: Saves original BGP prefixes
- Collapsed: Aggregates overlapping/adjacent prefixes into shortest form using Python's
collapse_addresses() - Split (optional): Normalizes IPv4 to /24 blocks and IPv6 to /48 blocks using
subnets()method
- Sorts and Saves: Outputs clean, sorted prefix lists to text files
- RIR Delegated Files:
- BGP Data: bgp.tools
This project is licensed under the MIT License.
Contributions are welcome! Feel free to fork the repository and submit a pull request.