浅谈筛选日志中的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 10:09
下一篇 2016-07-22 10:10

相关推荐

  • 优云软件老司机:如何让运维操作更轻松、高效

    讲师介绍 庞辉富 •广通软件技术总监 •拥有10多年IT运维管理软件研发经验 •致力于自动化运维解决方案的研究和推广 •主导研发的产品广泛应用于海关、公安、能源等多个行业 技术发展给运维带来的挑战 当前的IT建设在这些新技术的演进下,我们看到的是呈现“双态IT”特征。Gartner也提出双模IT理论,与现在谈的双态IT是异曲同工的,不再是一种单纯的形态,而是…

    系统运维 2017-01-09
  • Hadoop Hive与Hbase整合+thrift

    1.  简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句转换为MapReduce任务进行运行。 其优点是学习成本低,可以通过类SQL语句快速实现简单的MapReduce统计,不必开发专门的MapReduce应用,十分适合数据仓库的统计分析。 Hive与HBas…

    Linux干货 2015-04-13
  • 前三天基础-Linux文件系统概论

    Linux之我见     在2016农历丙申年2016年猴年,对于小白的我来说接触到了江湖流传的武功秘籍之-Linux心法,第一次接触到命令行,知道在键盘上随便敲击命令就可以满屏跑数据,就像看过的美国大片里黑客帝国里面的画面,感觉学会好后可以装逼了,废话不多说了,允许在下斗胆介绍Linux文件系统。     &nbsp…

    Linux干货 2016-03-24
  • 【福利贴-招聘】- python运维开发工程师

    职位描述  岗位职责: 1、负责公司CMDB的建设,包括资产管理、dns、工单系统、ngnix自动化、监控等的开发 2、帮助运维团队和业务团队提高自动化效率 3、维护和开发持续化集成环境; 4、负责公司运维系统的规划、选型、部署上线和日常维护管理工作; 5、负责CMDB团队的建设、管理和人才培养机制…

    Linux干货 2015-04-03
  • 为什么中国的网页设计那么烂?

    Nick Johnson,一个有12年经验的Web设计师在它的blog里写下了“Why is Chinese Web Design So Bad”,新浪,人人,百度,阿里巴巴,腾讯榜上有名。其中的观点相当的好,希望所有的中国人都读一下。我不全文翻译了,只是给大家看一些摘要。(保证不会像《环球时报》一样) —————————— 作者2005年的夏天来到中国,他…

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

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

    Linux干货 2016-10-30