保存ip的文件,是从nginx日志里面提取出来的,格式为:ipaddr request_timephp
ips.txt:nginx
99.248.29.64 0.464json
99.248.29.64 0.467api
99.248.29.64 0.469ide
99.248.29.64 0.489url
99.249.155.70 0.691spa
99.255.102.207 0.345日志
99.255.102.207 0.346code
99.255.102.207 0.349ip
99.255.102.207 0.354
99.255.102.207 0.360
99.255.102.207 0.361
99.255.102.207 0.499
99.255.102.207 0.501
99.255.102.207 0.516
99.255.102.207 0.520
99.255.102.207 0.520
99.255.102.207 0.606
99.255.102.207 0.612
99.255.102.207 0.687
....................
ipapi.php:
- <?php
- function getIsp($ipstr)
- {
- $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ipstr;
- $ip=json_decode(file_get_contents($url));
- if((string)$ip->code=='1'){
- return false;
- }
- $data = (array)$ip->data;
- return $data["isp"];
- }
- $fh=fopen("$argv[1]","r");
- while(!feof($fh)){
- $line = fgets($fh);
- $strings = explode(" ",$line);
- $ip = $strings[0];
- $time = $strings[1];
- $allip=array();
- if(!array_key_exists("$ip",$allip)){
- $yunying=getIsp($ip);
- $allip["$ip"]=$yunying;
- }else {
- $yunying=$allip["$ip"];
- }
- print $yunying."\t".$ip."\t".$time;
- }
- fclose($fh);
- ?>
脚本使用方法:/usr/bin/php ipapi.php ips.txt