LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 854|回复: 2

请问一个合并相同字段的shell命令

[复制链接]
发表于 2006-2-20 11:40:34 | 显示全部楼层 |阅读模式
通过查看log日志查出ip地址
cat test.log |grep sshd|grep failure|grep -v Failed|awk '{print $11}' |cut -c 7-21|grep -v 219.142.174.152|grep -v 210.51.7 > test.txt

然后有很多重复的,我想要整合相同的ip该用什么命令阿?半天也没想到。。。

cat test.txt

202.115.142.158
202.115.142.158
202.115.142.158
219.142.174.151
219.142.174.151
219.142.174.151
发表于 2006-2-20 12:30:02 | 显示全部楼层
  1. sort and then uniq -c
复制代码
w/o sort also work, but just nicer if you sort the output first.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-20 13:37:47 | 显示全部楼层
多谢了,已经可以了

#! /bin/bash
m=`date +%b`;
d=`date +%d`;
cat /var/log/messages |grep $m |grep $d > test.txt;
cat test.txt |grep sshd|grep failure|grep -v Failed|awk '{print $13}' |cut -c 7-21|grep -v 219.142.174.152|grep -v 210.51.7|sort|uniq|grep -v -|grep -v [a-z]>iptables.txt;
for i in  `sort -u iptables.txt`;
do
/sbin/iptables -I INPUT -s $i -p tcp --dport 22 -j DROP;
done

echo  iptables update ok!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表