PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Peter Kahl   PHP IP to ASN Mapping   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP IP to ASN Mapping
Map IPv4 or IPv6 address to ASN
Author: By
Last change: Update of README.md
Date: 3 years ago
Size: 2,572 bytes
 

Contents

Class file image Download

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"