2014-02-14 55 views
1

如何從一個文件中搜索這些線路,並採取只有最後一個IP地址:BASH - 我如何獲取IP地址並製作預期列表?

2014-02-14 06:42:00.527219 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.198.82 
2014-02-14 06:50:44.967314 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.162.250 
2014-02-14 06:54:38.587312 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.198.82 
2014-02-14 07:05:32.667277 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.198.82 
2014-02-14 07:10:08.067256 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 188.138.118.22 
2014-02-14 07:16:29.747256 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.198.82 
2014-02-14 07:30:16.587253 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 188.138.118.22 
2014-02-14 07:46:10.727254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:11.247254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:11.767254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:12.267221 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:12.767224 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:13.307251 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:13.767254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:14.587252 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:15.267221 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:16.007254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:16.507251 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:20.347236 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 07:46:20.807254 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 08:01:18.467226 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 188.138.118.22 
2014-02-14 08:32:18.127200 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 188.138.118.22 
2014-02-14 09:00:29.967234 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.162.250 
2014-02-14 09:03:13.207173 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 188.138.118.22 
2014-02-14 09:07:35.747256 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:36.187216 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:36.627217 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:37.067262 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:37.507219 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:37.927256 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:38.307205 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:38.947256 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:39.587246 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:40.327255 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:40.767255 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:41.207189 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:41.667163 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:07:42.107255 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 207.244.67.213 
2014-02-14 09:14:53.367170 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 172.246.162.250 
2014-02-14 09:18:57.127288 [WARNING] sofia_reg.c:2701 Can't find user [[email protected]] from 199.115.112.66 

從上面做一個列表如下:

-A INPUT -s 176.58.71.212/32 -j DROP 
.. 

嘗試:但不工作像預期

grep "Can't find user" /usr/local/freeswitch/log/freeswitch.log | awk '{print $10}' | xargs echo "-A \n" 

回答

2

簡單的解決方案如下: -

grep "Can't find user" /usr/local/freeswitch/log/freeswitch.log | \ 
awk '{print "-A INPUT -s " $10 "-j DROP"}' 

根據評論中的要求,您想刪除重複項。這是可以實現如下: -

grep "Can't find user" /usr/local/freeswitch/log/freeswitch.log | \ 
awk '{print "-A INPUT -s " $10 "-j DROP"}' | sort -u 

如前所述在評論中還有其他方法可以做到這一點稍長解釋也可能稍快執行。 awk可以匹配模式本身,這意味着我們不需要grep。這可以如下完成: -

awk '/find user/ {print "-A INPUT -s " $NF "-j DROP"}' /usr/local/freeswitch/log/freeswitch.log 

N.B.我使用了字符串「find user」而不是「Can not find user」來避免字符串轉義問題。

您也可以使用awk變量$ NF(字段數)使腳本更健壯一些。

+3

+1,但我寧願用'$ NF'比'$ 10'只是爲了安全起見。您也可以使用'awk'/無法找到用戶/ {。 。 。 }'/ usr/local/freeswitch/log/freeswitch.log'而不是'grep'。 –

+1

而不是將'grep'輸出到'awk',而是使用awk來過濾文本。 – devnull

+0

@devnull [+1](http://partmaps.org/era/unix/award.html#grep) – tripleee

2

試試這個,以避免DUP的

awk '/Can'"'"'t find user/ && !x[$NF]++ { print "-A INPUT -s " $NF "-j DROP" }' /usr/local/freeswitch/log/freeswitch.log 
+1

因此突出顯示在這裏給出了一個錯誤。我認爲你需要以某種方式逃避單引號,或者雙引號,並且逃避'$ NF' – BroSlow

+0

Thanks @BroSlow。接得好!! :) –

+1

如果使用bash,你可能需要逃避!以及。這將被解釋爲bash歷史事件。 –