Skip to content

Instantly share code, notes, and snippets.

@robot56
Last active December 28, 2015 11:38
Show Gist options
  • Save robot56/7494316 to your computer and use it in GitHub Desktop.
Save robot56/7494316 to your computer and use it in GitHub Desktop.
Shocky IPv4 Lookup
<?php
if(!$args) {
die("{IPv4} [ -country | -city | -region | -org | -phone | -loc ]");
}
$ip = gethostbyname($arg[0]);
if(!getIpValid($ip)) {
die("The requested IPv4 address is not valid.");
}
if(getCmdArgs($args)) {
foreach ($meta as $get) {
switch ($get) {
case '-country':
printf("\00302Country\003: %s ", getIpCountry($json));
break;
case '-city':
printf("\00302City\003: %s ", getIpCity($json));
break;
case '-region':
printf("\00302Region\003: %s ", getIpRegion($json));
break;
case '-phone':
printf("\00302Phone\003: %s ", getIpPhone($json));
break;
case '-loc':
printf("\00302Geo Location\003: %s ", getIpLoc($json));
break;
case '-org':
printf("\00302Organization\003: %s ", getIpOrg($json));
break;
default:
die("Error!");
break;
}
}
die();
}
printf("%s: ", $json['ip']);
printf("\00302HOSTNAME\003: %s | ", getIpHost($json));
printf("\00302COUNTRY\003: %s | ", getIpCountry($json));
printf("\00302REGION\003: %s | ", getIpRegion($json));
printf("\00302CITY\003: %s | ", getIpCity($json));
printf("\00302ORGANIZATION\003: %s | ", getIpOrg($json));
printf("\00302POSTAL\003: %s | ", getIpPostal($json));
printf("\00302PHONE\003: %s | ", getIpPhone($json));
printf("\00302GEO LOCATION\003: %s", getIpLoc($json));
//Check if IP is valid
function getIpValid($ip) {
$api = "http://ipinfo.io/";
global $json;
try {
$json = json_decode(
Requests::get(
$api.$ip."/json"
)->body, true
);
if(!$json['ip']){
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
//Get any extra arguments
function getCmdArgs($args) {
global $meta;
if(!preg_match("#\s-(.*)#", $args, $results)){
return false;
}
$meta = explode(" ", $results[0]);
return true;
}
//
function getIpHost($json) {
if(!is_null($json['hostname'])) {
return $json['hostname'];
}
return "n/a";
}
function getIpCountry($json) {
if(!is_null($json['country'])) {
return $json['country'];
}
return "n/a";
}
function getIpRegion($json) {
if(!is_null($json['region'])) {
return $json['region'];
}
return "n/a";
}
function getIpCity($json) {
if(!is_null($json['city'])) {
return $json['city'];
}
return "n/a";
}
function getIpOrg($json) {
if(!is_null($json['org'])) {
return $json['org'];
}
return "n/a";
}
function getIpPostal($json) {
if(isset($json['postal'])) {
return $json['postal'];
}
return "n/a";
}
function getIpPhone($json) {
if(isset($json['phone'])) {
return $json['phone'];
}
return "n/a";
}
function getIpLoc($json) {
if(!is_null($json['loc'])) {
return $json['loc'];
}
return "n/a";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment