利用shell 和 python统计日志里ip地区范围

这是分析ip地址区域的, 唯一的缺点就是速度慢~  他每次都curl到ip138网站上去~   这里主要是给大家一个思路~~~~ 扩展一下~

下面的例子是分析nginx的日志~  你也可以分析apache、varnish、squid 等等的日志~

  
  
  
  
  1. #!/bin/bash 
  2. IPSUMFILE=access_ip.log 
  3. SAVEFILE=ipaddress.txt 
  4.  
  5. echo -e "IP地址\t\t\t\t\t来源\n----------------------------------------------------">$SAVEFILE 
  6. LOG_DIR=/usr/local/nginx/logs 
  7. for i in $LOG_DIR/*.log 
  8. do 
  9.  FILE=`basename $i` 
  10.  awk '!a[$1]++{print $1}' $i >> $IPSUMFILE 
  11. done 
  12. #get ip souce 
  13. for i in `cat access_ip.log` 
  14. do 
  15.  wget http://www.ip168.com/ip/?ip=$i &>/dev/null 
  16.  address=`cat index.html\?ip\=$i | grep 本站主数据|sed 's/<div>.*:\(.*\)<.*>/\1 /'` 
  17.   
  18.  if [ "$address" == "" ];then 
  19.    echo -n "Failed get $i source,please check..." 
  20.  fi 
  21. echo "$i$address" >> $SAVEFILE 
  22. rm -rf index.html\?ip\=$i 
  23. done 
  24. #Convert Dos text files to the Unix format 
  25. which dos2unix &>/dev/null 
  26. if [ $? != 0 ];then 
  27. #yum install dos2unix 
  28. fi 
  29. dos2unix ipaddress.txt 
  30. echo “All work done.” 

如果日志极大的话~ 推荐使用python的方法~  用的是纯真ip库~ 

 

  
  
  
  
  1. #!/usr/bin/python 
  2.   
  3. from struct import * 
  4. import string 
  5.   
  6. def ip2string( ip ): 
  7.     a = (ip & 0xff000000) >> 24 
  8.     b = (ip & 0x00ff0000) >> 16 
  9.     c = (ip & 0x0000ff00) >> 8 
  10.     d = ip & 0x000000ff 
  11.     return "%d.%d.%d.%d" % (a,b,c,d) 
  12.   
  13. def string2ip( str ): 
  14.     ss = string.split(str, '.'); 
  15.     ip = 0L 
  16.     for s in ss: ip = (ip << 8) + string.atoi(s) 
  17.     return ip; 
  18.   
  19. class IpLocater : 
  20.     def __init__( self, ipdb_file ): 
  21.         self.ipdb = open( ipdb_file, "rb" ) 
  22.         # get index address  
  23.         str = self.ipdb.read( 8 ) 
  24.         (self.first_index,self.last_index) = unpack('II',str) 
  25.         self.index_count = (self.last_index - self.first_index) / 7 + 1 
  26.   
  27.     def getString(self,offset = 0): 
  28.         if offset : 
  29.             self.ipdb.seek( offset ) 
  30.         str = "" 
  31.         ch = self.ipdb.read( 1 ) 
  32.         (byte,) = unpack('B',ch) 
  33.         while byte != 0: 
  34.             strstr = str + ch 
  35.             ch = self.ipdb.read( 1 ) 
  36.             (byte,) = unpack('B',ch)  
  37.         return str 
  38.   
  39.     def getLong3(self,offset = 0): 
  40.         if offset : 
  41.             self.ipdb.seek( offset ) 
  42.         str = self.ipdb.read(3) 
  43.         (a,b) = unpack('HB',str) 
  44.         return (b << 16) + a 
  45.   
  46.     def getAreaAddr(self,offset=0): 
  47.         if offset : 
  48.             self.ipdb.seek( offset ) 
  49.         str = self.ipdb.read( 1 ) 
  50.         (byte,) = unpack('B',str) 
  51.         if byte == 0x01 or byte == 0x02: 
  52.             p = self.getLong3() 
  53.             if p: 
  54.                 return self.getString( p ) 
  55.             else: 
  56.                 return "" 
  57.         else: 
  58.             return self.getString( offset ) 
  59.   
  60.     def getAddr(self,offset ,ip = 0): 
  61.         self.ipdb.seek( offset + 4) 
  62.   
  63.         countryAddr = "" 
  64.         areaAddr = "" 
  65.         str = self.ipdb.read( 1 ) 
  66.         (byte,) = unpack('B',str) 
  67.         if byte == 0x01: 
  68.             countryOffset = self.getLong3() 
  69.             self.ipdb.seek(countryOffset ) 
  70.             str = self.ipdb.read( 1 ) 
  71.             (b,) = unpack('B',str) 
  72.             if b == 0x02: 
  73.                 countryAddr = self.getString( self.getLong3() ) 
  74.                 self.ipdb.seek( countryOffset + 4 ) 
  75.             else: 
  76.                 countryAddr = self.getString( countryOffset ) 
  77.             areaAddr = self.getAreaAddr() 
  78.         elif byte == 0x02: 
  79.             countryAddr = self.getString( self.getLong3() ) 
  80.             areaAddr = self.getAreaAddr( offset + 8 ) 
  81.         else: 
  82.             countryAddr = self.getString( offset + 4 ) 
  83.             areaAddr = self.getAreaAddr( ) 
  84.   
  85.         return countryAddr + "/" + areaAddr 
  86.   
  87.     def output(self, first ,last ): 
  88.         if last > self.index_count : 
  89.             last = self.index_count 
  90.         for index in range(first,last): 
  91.             offset = self.first_index + index * 7 
  92.             self.ipdb.seek( offset ) 
  93.             buf = self.ipdb.read( 7 ) 
  94.             (ip,of1,of2) = unpack("IHB",buf) 
  95.             print "%s - %s" % (ip, self.getAddr( of1 + (of2 << 16) ) ) 
  96.   
  97.     def find(self,ip,left,right): 
  98.         if right-left == 1: 
  99.             return left 
  100.         else: 
  101.             middle = ( left + right ) / 2 
  102.             offset = self.first_index + middle * 7 
  103.             self.ipdb.seek( offset ) 
  104.             buf = self.ipdb.read( 4 ) 
  105.             (new_ip,) = unpack("I",buf) 
  106.             if ip <= new_ip : 
  107.                 return self.find( ip, left, middle ) 
  108.             else: 
  109.                 return self.find( ip, middle, right ) 
  110.   
  111.     def getIpAddr(self,ip): 
  112.         index = self.find( ip,0,self.index_count - 1 ) 
  113.         ioffset = self.first_index + index * 7 
  114.         aoffset = self.getLong3( ioffset + 4) 
  115.         address = self.getAddr( aoffset ) 
  116.         return address                 
  117.   
  118. if __name__ == "__main__" : 
  119.     ip_locater = IpLocater( "QQWry.Dat" ) 
  120.     ip_locater.output( 100, 120 ) 
  121.     ip = '59.64.234.174' 
  122.     ip = '58.38.139.229' 
  123.     address = ip_locater.getIpAddr( string2ip( ip ) ) 
  124.     print "the ip %s come from %s" % (ip,address) 

 

 

你可能感兴趣的:(shell,日志)