Awstats配置GeoIP和纯真IP库显示IP地理位置信息

1.GeoIP插件安装:

[php]

#安装Perl组件
perl -MCPAN -e 'install Geo::IP::PurePerl'
#下载GeoIP国家、城市数据库、AS数据库
wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://www.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
gzip -d GeoLiteCity.dat.gz
gzip -d GeoIP.dat.gz
gzip -d GeoIPASNum.dat.gz

[/php]

编辑Awstats配置文件加载插件:

[php]

LoadPlugin="geoip GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugins/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugin/GeoLiteCity.dat
LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /usr/local/awstats/wwwroot/cgi-bin/plugins/GeoIPASNum.dat"

[/php]

2.纯真IP库插件安装:

cd /usr/local/awstats/wwwroot/cgi-bin/plugins

vim qqwry.pl

[php]

sub ipwhere {
my $ipbegin,$ipend,$ipData1,$ipData2,$DataSeek,$ipFlag;

my $ip=shift;
my @ip=split(/\./,$ip);
my $ipNum = $ip[0]*16777216+$ip[1]*65536+$ip[2]*256+$ip[3];

my $ipfile="./QQWry.Dat";
open(FILE,"$ipfile");
binmode(FILE);
sysread(FILE,$ipbegin,4);
sysread(FILE,$ipend,4);
$ipbegin=unpack("L",$ipbegin);
$ipend=unpack("L",$ipend);
my $ipAllNum = ($ipend-$ipbegin)/7+1;

my $BeginNum=0;
my $EndNum=$ipAllNum;

Bgn:
my $Middle= int(($EndNum+$BeginNum)/2);

seek(FILE,$ipbegin+7*$Middle,0);
read(FILE,$ipData1,4);
my $ip1num=unpack("L",$ipData1);
if ($ip1num > $ipNum) {
$EndNum=$Middle;
goto Bgn;
}

read(FILE,$DataSeek,3);
$DataSeek=unpack("L",$DataSeek."\0");
seek(FILE,$DataSeek,0);
read(FILE,$ipData2,4);
my $ip2num=unpack("L",$ipData2);
if ($ip2num < $ipNum) {
goto nd if ($Middle==$BeginNum);
$BeginNum=$Middle;
goto Bgn;
}

$/="\0";
read(FILE,$ipFlag,1);
if ($ipFlag eq "\1") {
my $ipSeek;
read(FILE,$ipSeek,3);
$ipSeek = unpack("L",$ipSeek."\0");
seek(FILE,$ipSeek,0);
read(FILE,$ipFlag,1);
}
if ($ipFlag eq "\2") {
my $AddrSeek;
read(FILE,$AddrSeek,3);
read(FILE,$ipFlag,1);
if($ipFlag eq "\2") {
my $AddrSeek2;
read(FILE,$AddrSeek2,3);
$AddrSeek2 = unpack("L",$AddrSeek2."\0");
seek(FILE,$AddrSeek2,0);
}
else {
seek(FILE,-1,1);
}
$ipAddr2=<FILE>;
$AddrSeek = unpack("L",$AddrSeek."\0");
seek(FILE,$AddrSeek,0);
$ipAddr1=<FILE>;
}
else {
seek(FILE,-1,1);
$ipAddr1=<FILE>;
read(FILE,$ipFlag,1);
if($ipFlag eq "\2") {
my $AddrSeek2;
read(FILE,$AddrSeek2,3);
$AddrSeek2 = unpack("L",$AddrSeek2."\0");
seek(FILE,$AddrSeek2,0);
}
else {
seek(FILE,-1,1);
}
$ipAddr2=<FILE>;
}

nd:
chomp($ipAddr1,$ipAddr2);
$/="\n";
close(FILE);

$ipAddr2="" if($ipAddr2=~/http/i);
my $ipaddr="$ipAddr1 $ipAddr2";
$ipaddr =~ s/CZ88\.NET//isg;
$ipaddr="未知地区" if ($ipaddr=~/未知|http/i || $ipaddr eq "");
return $ipaddr;
}

sub osinfo {
local $os="",$Agent;
$Agent = $ENV{'HTTP_USER_AGENT'};
if (($Agent =~ /win/i)&&($Agent =~ /95/i)) {
$os="Windows 95";
}
elsif (($Agent =~ /win 9x/i)&&($Agent =~ /4.90/i)) {
$os="Windows ME";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /98/i)) {
$os="Windows 98";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /nt 5\.0/i)) {
$os="Windows 2000";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /nt 5\.1/i)) {
$os="Windows XP";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /nt 5\.2/i)) {
$os="Windows 2003";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /nt/i)) {
$os="Windows NT";
}
elsif (($Agent =~ /win/i)&&($Agent =~ /32/i)) {
$os="Windows 32";
}
elsif ($Agent =~ /linux/i) {
$os="Linux";
}
elsif ($Agent =~ /unix/i) {
$os="Unix";
}
elsif (($Agent =~ /sun/i)&&($Agent =~ /os/i)) {
$os="SunOS";
}
elsif (($Agent =~ /ibm/isg)&&($Agent =~ /os/isg)) {
$os="IBM OS/2";
}
elsif (($Agent =~ /Mac/i)&&($Agent =~ /PC/i)) {
$os="Macintosh";
}
elsif ($Agent =~ /FreeBSD/i) {
$os="FreeBSD";
}
elsif ($Agent =~ /PowerPC/i) {
$os="PowerPC";
}
elsif ($Agent =~ /AIX/i) {
$os="AIX";
}
elsif ($Agent =~ /HPUX/i) {
$os="HPUX";
}
elsif ($Agent =~ /NetBSD/i) {
$os="NetBSD";
}
elsif ($Agent =~ /BSD/i) {
$os="BSD";
}
elsif ($Agent =~ /OSF1/i) {
$os="OSF1";
}
elsif ($Agent =~ /IRIX/i) {
$os="IRIX";
}
elsif ($Agent =~ /google/i) {
$os = "GoogleBot";
}
elsif ($Agent =~ /Yahoo/i) {
$os = "YahooBot";
}
$os = "Unknown" if ($os eq '');
$os =~ s/[\a\f\n\e\0\r\t\)\(\*\+\?]//isg;
$os = substr($os, 0, 15) if (length($os) > 15);
return $os;
}
sub browseinfo {
my $browser = "";
my $browserver = "";
my ($Agent, $Part, $browseinfo);
$Agent = $ENV{"HTTP_USER_AGENT"};

if ($Agent =~ /Lynx/i)
{
$browser = "Lynx";
}
elsif ($Agent =~ /MOSAIC/i)
{
$browser = "MOSAIC";
}
elsif ($Agent =~ /AOL/i)
{
$browser = "AOL";
}
elsif ($Agent =~ /Lynx/i)
{
$browser = "Lynx";
}
elsif ($Agent =~ /Opera/i)
{
$browser = "Opera";
}
elsif ($Agent =~ /JAVA/i)
{
$browser = "JAVA";
}
elsif ($Agent =~ /MacWeb/i)
{
$browser = "MacWeb";
}
elsif ($Agent =~ /WebExplorer/i)
{
$browser = "WebExplorer";
}
elsif ($Agent =~ /OmniWeb/i)
{
$browser = "OmniWeb";
}
elsif ($Agent =~ /Mozilla/i)
{
if ($Agent =~ "MSIE")
{
if ($Agent =~ /MyIE(\d*)/)
{
$browserver = $1;
$browser = "MyIE";
}
else
{
$Part = (split(/\(/, $Agent))[1];
$Part = (split(/\;/,$Part))[1];
$browserver = (split(/ /,$Part))[2];
$browserver =~ s/([\d\.]+)/$1/isg;
$browser = "Internet Explorer";
}
}
elsif ($Agent =~ "Opera")
{
$Part = (split(/\(/, $Agent))[1];
$browserver = (split(/\)/, $Part))[1];
$browserver = (split(/ /,$browserver))[2];
$browserver =~ s/([\d\.]+)/$1/isg;
$browser = "Opera";
}
else
{
$Part = (split(/\(/, $Agent))[0];
$browserver = (split(/\//, $Part))[1];
$browserver = (split(/ /,$browserver))[0];
$browserver =~ s/([\d\.]+)/$1/isg;
$browser = "Netscape Navigator";
}
}
elsif ($Agent =~ /google/i)
{
$browser = "GoogleBot";
}
elsif ($Agent =~ /Yahoo/i)
{
$browser = "YahooBot";
}

if ($browser ne '')
{
$browserver =~ s/[^0-9\.b]//isg;
$browserver = &lbhz($browserver, 4) if (length($browserver) > 10);
$browseinfo = "$browser $browserver";
}
else
{
$browseinfo = "Unknown";
}
$browseinfo =~ s/[\a\f\n\e\0\r\t\)\(\*\+\?]//isg;
$browseinfo =~ s/[\a\f\n\e\0\r\t\)\(\*\+\?]//isg;
$browseinfo = substr($browseinfo, 0, 28) if (length($browseinfo) > 28);
return $browseinfo;
}
1;

[/php]

vim qqhostinfo.pm

[php]

#!/usr/bin/perl
#-----------------------------------------------------------------------------
# HostInfo AWStats plugin
# This plugin allow you to add information on hosts, like a whois fields.
#-----------------------------------------------------------------------------
# Perl Required Modules: XWhois
#-----------------------------------------------------------------------------
# $Revision: 1.12 $ - $Author: eldy $ - $Date: 2004/03/27 18:09:00 $

# <-----
# ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
push @INC, "${DIR}/plugins";
# ----->
use strict;no strict "refs";

require "${DIR}/plugins/qqwry.pl";

#-----------------------------------------------------------------------------
# PLUGIN VARIABLES
#-----------------------------------------------------------------------------
# <-----
# ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
my $PluginNeedAWStatsVersion="6.0";
my $PluginHooksFunctions="ShowInfoHost";
# ----->

# <-----
# IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
use vars qw/
/;
# ----->

#-----------------------------------------------------------------------------
# PLUGIN FUNCTION: Init_pluginname
#-----------------------------------------------------------------------------
sub Init_qqhostinfo {
my $InitParams=shift;
my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion);

# <-----
# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
debug(" InitParams=$InitParams",1);
# ----->

return ($checkversion?$checkversion:"$PluginHooksFunctions");
}

#-----------------------------------------------------------------------------
# PLUGIN FUNCTION: ShowInfoHost_pluginname
# UNIQUE: NO (Several plugins using this function can be loaded)
# Function called to add additionnal columns to the Hosts report.
# This function is called when building rows of the report (One call for each
# row). So it allows you to add a column in report, for example with code :
# print "<TD>This is a new cell for $param</TD>";
# Parameters: Host name or ip
#-----------------------------------------------------------------------------
sub ShowInfoHost_qqhostinfo {
my $param="$_[0]";
# <-----
if ($param eq '__title__') {
print "<th width=\"80\">Location</th>";
}
elsif ($param) {
print "<td>";
print ipwhere("$param");
#print $param;
print "</td>";
}
else {
print "<td>&nbsp;</td>";
}
return 1;
# ----->
}

1; # Do not remove this line

[/php]

[php]

cd /usr/local/awstats/wwwroot/cgi-bin/plugins
#下载纯真IP数据库
wget http://update.cz88.net/soft/qqwry.rar
unrar qqwry.rar

[/php]

修改qqwry.pl内${DIR}为插件所在目录:

[php]

"${DIR}/plugins";
"${DIR}/plugins/qqwry.pl";

[/php]

修改qqhostinfo.pm内纯真IP数据库目录:

[php]my $ipfile="/PATH/QQWry.Dat";[/php]

最终效果如下:

1709014781

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 72 queries in 0.412 seconds