Skip to content

hatamiarash7/ASN-By-Country

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ASN By Country

made-with-python Python 3.11+ GitHub release License: MIT Code style: ruff

A Python tool to scrape ASN (Autonomous System Number), IPv4, and IPv6 allocation data by country code from RIR delegation statistics.

Features

  • 🌍 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

Installation

Using Make (recommended)

# 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 deps

Using pip

pip install -r requirements.txt

Using Docker

docker build -t asn-by-country .

Usage

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]

Optional Arguments

  • -d, --data-type <type>: Specify which type of data to fetch. The options are:

    • asn: Retrieve only AS numbers (default).
    • ipv4: Retrieve only IPv4 addresses.
    • ipv6: Retrieve only IPv6 addresses.
    • all: Retrieve AS numbers, IPv4 addresses, and IPv6 addresses.
  • -w, --max-workers <N>: Maximum concurrent workers (default: 5)

  • -q, --quiet: Suppress progress output

  • -v, --version: Show version

Examples

# 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

Using Makefile

# 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 help

Run with Docker

docker 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-build

Result

The output will be generated in the output_data directory:

File Name Description
{Country}_asn_list.csv Contains a list of ASN delegations for specified country.
{Country}_ipv4_list.csv Contains IPv4-specific ASN delegations for specified country.
{Country}_ipv6_list.csv Contains IPv6-specific ASN delegations for specified country.
asn_ranges.txt Contains a list of all ASN ranges across countries.
ipv4_ranges.txt Contains a list of all IPv4 ranges.
ipv6_ranges.txt Contains a list of all IPv6 ranges.

Development

Project Structure

ASN-By-Country/
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ cli.py              # Command-line interface
β”‚   β”œβ”€β”€ config.py           # Configuration constants
β”‚   β”œβ”€β”€ 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

Available Make Targets

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

Support πŸ’›

Donate with Bitcoin Donate with Ethereum

Contributing 🀝

Don't be shy and reach out to us if you want to contribute πŸ˜‰

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Issues

Each project may have many problems. Contributing to the better development of this project by reporting them. πŸ‘