浅谈筛选日志中的IP地址信息

作为运维人员,经常会需要会对日志中的某些重要信息进行筛选,比如说ip等参数。

案例一:筛选出IP地址信息

日志信息如下:

[root@C67-X64-A1 hanghang]# cat test.txt 
Jul 13 08:13:09 localhost sshd[14678]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:09 localhost sshd[14679]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.73.173.143 user=root
Jul 13 08:13:11 localhost sshd[14691]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 user=admin
Jul 13 08:13:11 localhost sshd[14692]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.73.173.143 
Jul 13 08:13:14 localhost sshd[14707]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:14 localhost sshd[14711]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.73.173.143 user=root
Jul 13 08:13:17 localhost sshd[14722]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:17 localhost sshd[14724]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.73.173.143 
Jul 13 08:13:20 localhost sshd[14739]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 user=root
Jul 13 08:13:23 localhost sshd[14753]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 user=root
Jul 13 08:13:26 localhost sshd[14767]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:29 localhost sshd[14781]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:32 localhost sshd[14795]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:35 localhost sshd[14809]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:38 localhost sshd[14823]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:41 localhost sshd[14837]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 user=apache
Jul 13 08:13:44 localhost sshd[14851]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:47 localhost sshd[14865]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:49 localhost sshd[14876]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172 
Jul 13 08:13:53 localhost sshd[14895]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.152.95.172

方法1:利用awk命令进行筛选

[root@C67-X64-A1 hanghang]# awk -F "rhost=" '{print $NF}' test.txt |awk '{print $1'}|sort -r|uniq
61.152.95.172
222.73.173.143

方法2:利用grep的扩展命令egrep进行筛选

[root@C67-X64-A1 hanghang]# egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' test.txt |sort -r|uniq
61.152.95.172
222.73.173.143

方法3:利用sed命令进行筛选

[root@C67-X64-A1 hanghang]# sed -nr 's/.*[^0-9](([0-9]+\.){3}[0-9]+).*/\1/p' test.txt |sort -r|uniq
61.152.95.172
222.73.173.143
[root@C67-X64-A1 hanghang]# sed -nr 's/(^|.*[^0-9])(([0-9]+\.){3}[0-9]+).*/\2/p' test.txt |sort -r|uniq
61.152.95.172
222.73.173.143

案例二:根据需求对日志信息进行筛选

需求:

最近需要处理下网站日志:

例如

A 1.1.1.1      用户访问  有index.html  和a.jpg   的日志

B 20.20.20.20  用户访问  有index.html  的日志 没其他文件记录的日志

现在需要提取B的IP    不需要A的IP 

日志信息如下:

[root@C67-X64-A1 hanghang]# cat files 
1.1.1.1 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
10.10.10.10 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
10.10.10.10  - - [19/Jul/2013:15:01:39 +0800] "GET /logo.jpg  HTTP/1.1
10.10.10.10  - - [19/Jul/2013:15:01:39 +0800] "GET /a.js  HTTP/1.1
3.3.3.3 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
20.20.20.20 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
20.20.20.20  - - [19/Jul/2013:15:01:39 +0800] "GET /logo.jpg  HTTP/1.1
20.20.20.20  - - [19/Jul/2013:15:01:39 +0800] "GET /a.js  HTTP/1.1
30.30.30.30 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
30.30.30.30  - - [19/Jul/2013:15:01:39 +0800] "GET /logo.jpg  HTTP/1.1
30.30.30.30  - - [19/Jul/2013:15:01:39 +0800] "GET /a.js  HTTP/1.1
4.4.4.4 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
5.5.5.5 - - [19/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
1.1.1.1 - - [20/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
2.2.2.2 - - [21/Jul/2013:15:01:39 +0800] "GET /index.html  HTTP/1.1
3.3.3.3 - - [21/Jul/2013:15:01:55 +0800] "GET /index.html  HTTP/1.1
4.4.4.4 - - [21/Jul/2013:16:01:55 +0800] "GET /index.html  HTTP/1.1
5.5.5.5 - - [21/Jul/2013:17:02:55 +0800] "GET /index.html  HTTP/1.1

Shell脚本实现:

#!/bin/bash
#author molewan
for i in `grep -v "/index.html"  files  | awk '{print $1}' | uniq`;do
    echo "| grep -v "$i" " >> tmp_title
done
M=`cat tmp_title | tr "\n" " " | sed 's#^#cat files | sort -r | uniq#'`
echo $M | bash | awk '{print $1}'
rm -rf tmp_title

Python脚本实现:

假设日志信息是放在文件log.dat里面的:

#! /usr/bin/env python                                                                
import re                     
Dip_reso = {}  
pattern = re.compile('(\d+\.\d+\.\d+\.\d+).*GET /(.*) .*')             
f = open('log.dat')
               
for line in f: 
    resource = re.match(pattern, line)                                 
    key = resource.group(1)                                            
    value = resource.group(2)                                          
    if key in Dip_reso:
        if value not in Dip_reso[key]:                                                
            Dip_reso[key].append(value)    
        else:
            continue           
    else:      
        Dip_reso[key] = []                                             
        Dip_reso[key].append(value)                                    
f.close()      
               
for k in Dip_reso:                                                     
    if len(Dip_reso[k]) == 1 and  cmp(Dip_reso[k][0], 'index.html') == 1:
        print k

                

#如果你要搜集数据,可以这样

# ip_data = [ip for ip in Dip_reso if len(Dip_reso[ip]) == 1 and cmp(Dip_reso[ip][0], 'index.html') == 1]

这样,ip_data就是所有的ip了。

原创文章,作者:Net21-冰冻vs西瓜,如若转载,请注明出处:http://www.178linux.com/24749

(1)
Net21-冰冻vs西瓜Net21-冰冻vs西瓜
上一篇 2016-07-22
下一篇 2016-07-22

相关推荐

  • Redis集群明细文档

    Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式)。目前根据文档已经看到Redis正在开发集群功能,其中一部分已经开发完成,但是具体什么时候可以用上,还不得而知。文档来源:http://redis.io/topics/cluster-spec 一、介绍   该文档是…

    Linux干货 2015-04-04
  • gzip压缩算法

    gzip,zlib,以及图形格式png,使用的是同一个压缩算法deflate。我们通过对gzip源码的分析来对deflate压缩算法做一个详细的说明: 第一,gzip压缩算法基本原理的说明。 第二,gzip压缩算法实现方法的说明。 第三,gzip实现源码级的说明。 1. Gzip压缩算法的原理      &n…

    Linux干货 2015-07-30
  • day6作业

    1、创建用户gentoo,附加组为bin和root,默认shell为 /bin/csh,注释信息为"Gentoo Distribution"     2、创建下面的用户、组和组成员关系 名字为admins 的组 用户natasha,使用admins …

    系统运维 2016-08-08
  • 二维码的生成细节和原理

    二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字,日文,中文等等。这两天学习了一下二维码图片生成的相关细节,觉得这个玩意就是一个密码算法,在此写一这篇文章 ,揭露一下。供好学的人一同学习之。 关于QR Code…

    Linux干货 2016-08-15
  • Linux命令的使用格式及部分常用命令详述

    Linux命令的使用格式及部分常用命令详述 Linux系统中命令的使用格式 Linux中命令的使用遵循以下格式 # COMMAND OPTIONS ARGUMENTS   ###命令 选项 参数,三项之间用空格分开 执行一个命令需要指定需要内核将哪一个二进制程序运行为一个进程,C…

    Linux干货 2016-10-30
  • zabbix报警信息提取

    zabbix报警信息提取     在日常的监控中,我们除了日常的zabbix操作外,我们有的时候还涉及到与其他公司进行数据对接。由于别的公司的数据对接很多时候并不是按照zabbix的数据结构(尤其是大型厂家,或是专业监控厂家,并不会直接使用zabbix,多数是自己开发或是对其他监控软件进行二次开发之类),在这种需求基础…

    Linux干货 2015-12-19