#!/usr/bin/php city($ip); if ($s = $city->country->names) $info .= $s['en']; else $info .= 'Unknown'; if ($s = $city->city->names) $info .= ',' . $s['en']; if ($s = $isp_db->isp($ip)->isp) $info .= ' - ' . preg_replace('/\s*[\-\(].+$/', '', $s); } catch (GeoIp2\Exception\AddressNotFoundException $e) { } if (!$info) return $ip; if (!$isatty) return "$ip($info)"; return "$ip\033[1;34m($info)\033[0m"; } $isatty = posix_isatty(STDOUT); while($line = fgets(STDIN)){ $pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\d{4}:[0-9a-f:]+)/'; echo preg_replace_callback($pattern, function($matches) { return ipinfo($matches[0], $GLOBALS['isatty']); }, $line); }