apache/nginxログからipのソースを分析し、データベースに書き込む方法


問題のように、分析はまず最も簡単なipを取り出します
cd /usr/local/nginx/logs
awk '{print $1}'  access.log | sort  |  uniq -c  | sort -rn | head -n 50 | awk '{print $2}' > /root/list
# nginx/apache        。 ip  list  

それから構想はすべてのipの出所を分析して、私が使った宝を洗うipデータベース、やはり悪くありません
#!/bin/bash

ipInfo() {
  for i in `cat list`
  do
    TransCoding="/usr/bin/python TransCoding.py"
    JsonDate="curl -s http://ip.taobao.com/service/getIpInfo.php?ip=$i"
    country=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==2{print $3}'`     area=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==4{print $2}'`     region=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==6{print $2}'`     city=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==8{print $2}'`     county=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==10{print $2}'`     isp=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==12{print $2}'`     printf "%-18s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s
" $i $country $isp $area $region $city $county   done } printf "%-18s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s
" IP             / echo -e "\e[1;33m======================================================================\e[0m" ipInfo

トランスコードコード
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import sys

def main():
    for line in sys.stdin:
        sys.stdout.write(re.sub(r'\\u\w{4}',
            lambda e: unichr(int(e.group(0)[2:], 16)).encode('utf-8'),
            line))

if __name__ == '__main__':
    main()

説明すると、shellはタオバオのapiからipアドレスのソースを取得することができますが、jsonのフォーマットで、pythonでutf 8のフォーマットに変換する必要があります.
このときipソースクエリーを解決するには、次はデータベースに書くことです.簡単です.shellを修正すればいいです.もちろん、データベースに行ってフィールドを手動で作成することが前提です.データ型は自分で定義すればokです.
+-----------------+---------+--------------+--------+--------------------------+-----------------+
52 rows in set (0.00 sec)

mysql> quit
Bye
You have new mail in /var/spool/mail/root
[root@10-6-12-175 ~]# vim ip1.sh 
#!/bin/bash

ipInfo() {
  for i in `cat list`
  do
    TransCoding="/usr/bin/python TransCoding.py"
    JsonDate="curl -s http://ip.taobao.com/service/getIpInfo.php?ip=$i"
    country=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==2{print $3}'`     area=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==4{print $2}'`     region=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==6{print $2}'`     city=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==8{print $2}'`     county=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==10{print $2}'`     isp=`$JsonDate | sed 's/,/
/g' | $TransCoding | tr -d "{}\"" | awk -F ":" 'NR==12{print $2}'`     printf "%-18s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s
" $i $country $isp $area $region $city $county     mysql -uroot -pxxxxxx  ip_address << EOF     use ip_address;     insert into ip_address values ("$i","$country","$isp","$area","$region","$city"); EOF   done } ipInfo

mysqlのクエリーインタフェースをください.
mysql> SELECT * FROM ip_address;
+-----------------+---------+--------------+--------+--------------------------+-----------------+
| ip              | country | isp          | area   | region                   | city            |
+-----------------+---------+--------------+--------+--------------------------+---------
| 101.199.112.45  |       |              |      |                       |              |
| 101.226.125.116 |       |            |      |                       |              |
| 101.226.33.199  |       |            |      |                       |              |
| 101.226.33.222  |       |            |      |                       |              |
| 101.226.61.142  |       |            |      |                       |              |
| 101.81.49.203   |       |            |      |                       |              |
| 106.120.160.109 |       |            |      |                       |              |
| 111.73.45.242   |       |            |      |                       |              |
| 111.73.46.22    |       |            |      |                       |              |
| 112.65.191.116  |       |            |      |                       |              |
| 115.231.218.126 |       |            |      |                       |              |
| 116.231.78.64   |       |            |      |                       |              |
| 117.131.99.203  |       |            |      |                       |              |
| 117.21.226.160  |       |            |      |                       |              |
| 119.161.140.147 |       |              |      |                       |              |
| 12.197.154.136  |       |              |        |                          |                 |
| 120.132.72.219  |       |          |      |                       |              |
| 120.204.200.42  |       |            |      |                       |              |
| 121.32.128.186  |       |            |      |                       |              |
| 121.40.83.80    |       |           |      |                       |              |
| 125.211.216.98  |       |            |      |                      |             |
| 140.207.30.50   |       |            |      |                       |              |
| 180.153.163.205 |       |            |      |                       |              |
| 180.153.213.141 |       |            |      |                       |              |
| 180.154.173.64  |       |            |      |                       |              |
| 180.173.226.55  |       |            |      |                       |              |
| 183.233.169.183 |       |            |      |                       |              |
| 183.61.171.132  |       |            |      |                       |              |
| 192.3.6.2       |       |              |        |                          |                 |
| 198.13.108.236  |       |              |        |                          |                 |
| 203.156.217.33  |       |              |      |                       |              |
| 203.171.238.69  |       |              |      |                       |              |
| 216.99.156.172  |       |              |        |                          |                 |
| 218.84.251.209  |       |            |      |                  |            |
| 220.181.132.193 |       |            |      |                       |              |
| 220.181.132.215 |       |            |      |                       |              |
| 221.2.205.150   |       |            |      |                       |              |
| 221.203.98.43   |       |            |      |                       |              |
| 222.174.5.25    |       |            |      |                       |              |
| 222.186.30.115  |       |            |      |                       |              |
| 222.186.30.82   |       |            |      |                       |              |
| 222.186.31.130  |       |            |      |                       |              |
| 222.186.31.149  |       |            |      |                       |              |
| 222.215.230.233 |       |            |      |                       |              |
| 223.104.3.186   |       |            |      |                       |              |
| 42.62.56.151    |       |          |      |                       |              |
| 50.7.37.98      |       |              |        |                          |                 |
| 58.217.255.4    |       |            |      |                       |              |
| 59.108.122.184  |       |          |      |                       |              |
| 61.148.243.106  |       |            |      |                       |              |
| 61.164.211.98   |       |            |      |                       |