简体   繁体   English

用awk删除字符

[英]Remove Characters with awk

I have a script that I'm working on to block IP address of failed login's on an SFTP server. 我正在使用一个脚本来阻止SFTP服务器上登录失败的IP地址。 I'm also using Centrify to hook the server into AD, and I've notice that Centrify logs users access differently than normal. 我还使用Centrify将服务器连接到AD,并且我注意到Centrify记录的用户访问权限与正常情况不同。 I'm able to pull up the IP address of failed logins with the following one liner: 我可以使用以下一种方法来提取登录失败的IP地址:

(grep sshd /var/log/messages | grep "AUTH_FAIL_PASSWD" | awk '{print $18}' | sort | uniq -c | awk '{ if ( $1 > 10) print $2 }')

With Centrify this gives the output of: 使用Centrify,可以提供以下输出:

client=0.0.0.0

What I need to do remove "client=" so that I can pass just the IP address to the rest of the script. 我需要删除“ client =“,以便仅将IP地址传递给脚本的其余部分。

Thanks for any help! 谢谢你的帮助!

Let's start be getting rid of all the useless pipes in your command line. 让我们开始摆脱命令行中所有无用的管道。 Change the whole thing to just this one awk command: 将整个内容更改为一个awk命令:

awk '/sshd/&&/AUTH_FAIL_PASSWD/{cnt[$18]++} END{for (ip in cnt) if (cnt[ip] > 10) print ip}' /var/log/messages

Now - update your question to include some sample input (contents of /var/log/messages) and expected output and tell us in terms of the text in those files what it is you're trying to do. 现在-更新您的问题,以包括一些示例输入(/ var / log / messages的内容)和预期输出,并根据这些文件中的文本告诉我们您正在尝试执行的操作。

Oh, I think I get it. 哦,我想我明白了。 This might be what you want: 这可能是您想要的:

awk '/sshd/&&/AUTH_FAIL_PASSWD/{sub(/.*=/,"",$18); cnt[$18]++} END{for (ip in cnt) if (cnt[ip] > 10) print ip}' /var/log/messages

You can just dump this back to awk: 您可以将其转储回awk:

(grep sshd /var/log/messages | grep "AUTH_FAIL_PASSWD" | awk '{print $18}' | sort | uniq -c | awk '{ if ( $1 > 10) print $2 }' | awk -F"=" '{ print $2 }') (grep sshd / var / log / messages | grep“ AUTH_FAIL_PASSWD” | awk'{print $ 18}'| sort | uniq -c | awk'{if($ 1> 10)print $ 2}'| awk -F“ =”' {打印$ 2}')

This last awk splits the string by an equals sign as the delimiter and prints the second element. 最后一个awk用等号将字符串分割为定界符,并输出第二个元素。

You didn't tell, but if the ip is $18 您没有告诉,但是如果IP是$18

... | awk '{if(split($18,a,/=/)==2) print a[2]; else print a[1] }' | ...

should do what you want 应该做你想做的

Even simpler 更简单

... | awk '{sub(/.*=/,"",$18); print $18}'  | ...

Further, I like pipes, in many cases they show clearly your intentions, but maybe it is worth accepting at least part of what other answers suggested 此外,我喜欢管道,在很多情况下,管道可以清楚地表明您的意图,但是也许值得接受至少其他建议的部分建议

 awk '/sshd/&&/AUTH_FAIL_PASSWD/ {
      sub(/.*-/,"",$18; print $18}' /var/log/messages | sort | ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM