PHP Classes

PHP IP to ASN Mapping: Map IPv4 or IPv6 address to ASN

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 154 All time: 9,040 This week: 560Up
Version License PHP version Categories
ip2asn 1.0.0Custom (specified...5Networking, PHP 5
Description 

Author

This class can map IPv4 or IPv6 address to ASN.

It can take a given IP address either in IPv4 or IPv6 format and get the autonomous system number for that IP address querying the cymru whois service.

The results are cached to avoid overhead of accessing the whois cymru service for the same IP ASN query.

Innovation Award
PHP Programming Innovation award nominee
July 2017
Number 6
An autonomous system is a part of network that is connected to other networks according to routing definitions.

An autonomous system number (ASN) is a number assigned to each network. A computer with an IP address belongs to a given network with its own ASN.

This class can determine what is the ASN of a given IPv4 or IPv6 address by calling the cymru whoi service that is now very huge.

Manuel Lemos
Picture of Peter Kahl
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 37 packages by
Country: United Kingdom
Age: ???
All time rank: 41521 in United Kingdom
Week rank: 22 Up2 in United Kingdom Up
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Documentation

ip2asn

Downloads Download per Month License If this project has business value for you then don't hesitate to support me with a small donation.

Maps IP address to ASN. ASN to prefix. ASN to name. Both IPv4 and IPv6.

Required

  • Writeable cache directory (permissions, ownership)
  • PHP functions `exec()` and `shell_exec()` are not disabled
  • Can execute bash scripts (using crontab)
  • Package `whois` installed
  • Package `dnsutils` installed

Installing Required Packages

sudo apt install whois dnsutils

Shell Scripts

Essential component of this library are 2 shell scripts.

Script asn-cache-purge.sh is used to purge cached data. You may want to run this script hourly using crontab. (Edit parameters as needed.)

Script update-asn2name.sh is used to download database file for mapping AS numbers to their names. You may want to run this script (not more than) once daily using crontab. (Edit parameters as needed.)

Usage

IP to ASN (and all related information)

use peterkahl\ip2asn\ip2asn;

$asnObj = new ip2asn('/srv/bgp'); # The argument is the cache directory.

$temp = $asnObj->getAsn('8.8.8.8'); # Accepts both IPv4 and IPv6

var_dump($temp);

/*
array(7) {
  ["as_number"]=>
  string(5) "15169"
  ["as_prefix"]=>
  string(10) "8.8.8.0/24"
  ["as_prefix_bin"]=>
  string(24) "000010000000100000001000"
  ["as_country_code"]=>
  string(2) "US"
  ["as_isp"]=>
  string(24) "GOOGLE - Google Inc., US"
  ["as_nic"]=>
  string(4) "ARIN"
  ["as_alloc"]=>
  string(0) "NA"
}
*/

AS Number(s) to List of Prefixes

use peterkahl\ip2asn\ip2asn;

$asnObj = new ip2asn('srv/bgp'); # The argument is the cache directory.

# This will get us an array of all prefixes for AS 94, 95, 96.
# The second argument defines your choice of IPv (4 or 6).
$temp = $asnObj->ArrayAsn2prefix(array(94, 95, 96), 6);

var_dump($temp);

AS Number to Name (Description)

use peterkahl\ip2asn\ip2asn;

$asnObj = new ip2asn('srv/bgp'); # The argument is the cache directory.

$temp = $asnObj->Asn2description(15169);

echo $temp; # "GOOGLE - Google Inc., US"

  Files folder image Files (6)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file ip2asn_purger.sh Data Auxiliary data
Accessible without login Plain text file ip2asn_updater.sh Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file ip2asn.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:154
This week:0
All time:9,040
This week:560Up