A Python tool to scrape ASN (Autonomous System Number), IPv4, and IPv6 allocation data by country code from RIR delegation statistics.
- Fetch network data for any country using ISO 3166-1 alpha-2 codes
- Support for ASN, IPv4, and IPv6 data types
- Multi-threaded fetching for improved performance
- Clean data output in CSV and text formats
- Rich console output with progress tracking
- Docker support for containerized execution
Data source: RIR Delegations & RIPE NCC Allocations
# Clone the repository
git clone https://github.com/hatamiarash7/ASN-By-Country.git
cd ASN-By-Country
# Create virtual environment and install all dependencies
make depspip install -r requirements.txtdocker build -t asn-by-country .python main.py <country_code_1> <country_code_2> ... [options]
# Or using the module
python -m src.cli <country_code_1> <country_code_2> ... [options]-
-d, --data-type <type>: Specify which type of data to fetch. The options are:asn: Retrieve only AS numbers.ipv4: Retrieve only IPv4 addresses.ipv6: Retrieve only IPv6 addresses.all: Retrieve AS numbers, IPv4 addresses, and IPv6 addresses (default).
-
-w, --max-workers <N>: Maximum concurrent workers (default: 5) -
-q, --quiet: Suppress progress output -
-v, --version: Show version
# Fetch ASN data for multiple countries
python main.py IR US FR
# Fetch only ASN data
python main.py IR --data-type asn
# Fetch all data types
python main.py IR US --data-type all
# Using quiet mode with more workers
python main.py IR US DE --quiet --max-workers 10# Create virtual environment and install dependencies
make deps
# Run the program
make run ARGS="US"
make run ARGS="IR US DE --data-type all"
# Run linters and type checker
make check
# Run tests with coverage
make test-cov
# Clean outputs
make clean
# Show all available targets
make helpdocker run --rm -v ./results:/app/output_data hatamiarash7/asn-by-country:latest <country_code_1> <country_code_2> ... [options]Using makefile:
make docker-run ARGS="IR"
make docker-run ARGS="US DE"Build docker image only:
make docker-buildThe output will be generated in the output_data directory:
| File Name | Description |
|---|---|
{Country}_asn_list.csv |
Contains a list of ASN delegations for the specified country. |
{Country}_ipv4_list.csv |
Contains IPv4-specific ASN delegations for the specified country. |
{Country}_ipv6_list.csv |
Contains IPv6-specific ASN delegations for the specified country. |
{Country}_ipv4.rsc |
Contains IPv4-specific MikroTik script for the specified country. |
{Country}_ipv6.rsc |
Contains IPv6-specific MikroTik script for the specified country. |
asn_prefixes.txt |
Contains a list of all ASN prefixes across countries. |
ipv4_prefixes.txt |
Contains a list of all IPv4 prefixes. |
ipv6_prefixes.txt |
Contains a list of all IPv6 prefixes. |
ipv4_expanded.txt |
Contains a list of all expanded IPv4 addresses. |
ASN-By-Country/
├── src/ # Source code
│ ├── cli.py # Command-line interface
│ ├── config.py # Configuration constants
│ ├── mikrotik.py # Mikrotik rsc generator
│ ├── models.py # Data models
│ ├── scraper.py # Web scraping logic
│ └── storage.py # File storage operations
├── tests/ # Unit tests
├── Dockerfile
├── Makefile
├── pyproject.toml # Project configuration
├── requirements.txt # Production dependencies
└── requirements-dev.txt # Development dependencies
| Target | Description |
|---|---|
make help |
Show all available targets |
make deps |
Install all dependencies |
make run ARGS="..." |
Run the application |
make lint |
Run all linters |
make format |
Format code |
make type-check |
Run type checking |
make check |
Run all code quality checks |
make test |
Run tests |
make test-cov |
Run tests with coverage |
make test-junit |
Run tests with JUnit report |
make docker-build |
Build Docker image |
make docker-run ARGS="..." |
Run in Docker |
make clean-all |
Remove all generated files |
Don't be shy and reach out to us if you want to contribute 😉
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request
Each project may have many problems. Contributing to the better development of this project by reporting them. 👍