Skip to content

Instantly share code, notes, and snippets.

@6footGeek
Created August 23, 2017 09:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6footGeek/59a8724c06328626c83d03efbb9f3341 to your computer and use it in GitHub Desktop.
Save 6footGeek/59a8724c06328626c83d03efbb9f3341 to your computer and use it in GitHub Desktop.
IP to ASN lookup PHP
private function getAsnFromIP($ip)
{
$query = 'whois -h whois.cymru.com " -f ' . (string) $ip . '"';
$whoisResult = shell_exec($query);
$asnArray = explode('|', $whoisResult);
$cleanAsn = array_map('trim', $asnArray);
return $cleanAsn;
}
@x011
Copy link

x011 commented May 20, 2022

function ip2asn($ip)
{
    return trim(shell_exec("whois -h whois.cymru.com $ip|sed 1,1d|cut -d '|' -f 1"));
}

$asn = ip2asn($_SERVER['REMOTE_ADDR']);

# 16247 https://ipinfo.io/AS16247
# 9009 https://ipinfo.io/AS9009

$bad_asn = array("16247", "9009");

if(in_array($asn, $bad_asn)){
    http_response_code(404);
    exit();
}

@x011
Copy link

x011 commented Mar 21, 2023

@victorelec14 You'll need to install PECL geoip >= 1.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment