PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-11-17 (29 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 72 This week: 2All time: 8,964 This week: 357Up
Version License PHP version Categories
ip2asn 1.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
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 32 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 6584 in Hong Kong Hong Kong
Week rank: 31 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 19x

Details

ip2asn

IP address intelligence. Maps IP address to ASN. ASN to prefix. ASN to name.

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 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  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file asn-cache-purge.sh Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file update-asn2name.sh Data Auxiliary data

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:72
This week:2
All time:8,964
This week:357Up