https://github.com/devanshbatham/ip2asn
A utility to quickly map IP addresses to their respective ASN
https://github.com/devanshbatham/ip2asn
Last synced: 10 months ago
JSON representation
A utility to quickly map IP addresses to their respective ASN
- Host: GitHub
- URL: https://github.com/devanshbatham/ip2asn
- Owner: devanshbatham
- License: mit
- Created: 2023-08-08T12:39:21.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-26T05:32:24.000Z (almost 3 years ago)
- Last Synced: 2025-07-23T16:38:01.433Z (11 months ago)
- Language: Python
- Homepage:
- Size: 3.39 MB
- Stars: 19
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ip2asn
A utility to quickly map IP addresses to their respective ASN
🏗️ Installation
⛏️ Usage
🔄 Updating RIB Data
📖 How It Works

# Installation
```sh
git clone https://github.com/devanshbatham/ip2asn
cd ip2asn
sudo chmod +x setup.sh
./setup.sh
```
*Note:* The utility comes pre-packed with trie data, so if you don't wish to update the RIB data, it should work well right off the bat.
# Usage
```sh
(~)>> cat input_ips.txt | ip2asn
74.122.191.97 [AS15211]
74.122.191.99 [AS15211]
151.101.65.49 [AS54113]
162.159.136.66 [AS13335]
75.2.48.152 [AS16509]
76.223.91.57 [AS16509]
80.239.194.78 [AS1299]
99.86.20.101 [AS16509]
99.86.20.118 [AS16509]
```
- With `--json` for JSON output:
```sh
(~)>> cat input_ips.txt | ip2asn --json
{
"AS1299": [
"62.115.144.49",
"62.115.37.34",
"62.115.57.70",
"80.239.194.78"
],
"AS11404": [
"65.50.201.2"
],
"AS11696": [
"69.60.198.202"
]
}
```
# Updating RIB Data
If you wish to build the trie yourself or want to update the trie data:
1. By default, `trie_data.json.gz` is saved in the `~/.ip2asn/` folder. So, firstly, delete the `~/.ip2asn` folder.
2. Run the `get-rib.sh` script.
3. Build the trie using the `--rib-path` or `--rp` option with the updated RIB data.
```sh
echo "8.8.8.8" | ip2asn --rib-path /path/to/processed_rib.txt
```
# How It Works
The `ip2asn` utility employs a trie data structure to map IP address prefixes to their corresponding Autonomous System Numbers (ASN). Each key in the trie represents an IP address prefix, while the corresponding value denotes the AS number that announces these prefixes. The utility checks this trie to find the longest matching prefix for each IP and retrieves its associated ASN.