保存ip的文件,是从nginx日志里面提取出来的,格式为:ipaddr request_time
ips.txt:
99.248.29.64 0.464
99.248.29.64 0.467
99.248.29.64 0.469
99.248.29.64 0.489
99.249.155.70 0.691
99.255.102.207 0.345
99.255.102.207 0.346
99.255.102.207 0.349
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
转载于:https://blog.51cto.com/cnhotfire/1176843
本文介绍如何从nginx日志中提取IP地址,并利用PHP脚本获取每个IP对应的运营商信息,通过分析日志文件实现IP地址与运营商名称的对应。
632

被折叠的 条评论
为什么被折叠?



