如何将 IP 地址列表重定向到命令行功能?

发布于 2025-01-10 20:58:11 字数 722 浏览 0 评论 0原文

我想查看哪些国家/地区正在尝试访问我的 VPS。我安装了一个名为“goiplookup”的工具,它是从另一个名为“geoiplookup”的项目中分叉出来的。如果我在命令行中输入:

goiplookup 8.8.8.8

它会返回:

US, United States

所以我想出了如何使用以下方法获取尝试访问我的服务器的 IP 列表:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}'

这给出了一个长长的 IP 列表,如下所示:

1.1.1.1
2.2.2.2
3.3.3.3

I无法弄清楚如何获取要由“goiplookup”工具处理的此 IP 列表。我尝试过这个:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | goiplookup

但这不起作用。我也尝试过但没有运气:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | xargs -0 goiplookup

I want to see what countries are trying to access my VPS. I have installed a tool called "goiplookup", which was forked from another effort called "geoiplookup". If I type this at the command line:

goiplookup 8.8.8.8

It returns this:

US, United States

So I figured out how to get a list of IPs that are trying to access my server by using this:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}'

Which gives a long list of IPs like this:

1.1.1.1
2.2.2.2
3.3.3.3

I cannot figure out how to get this list of IPs to be processed by the "goiplookup" tool. I tried this:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | goiplookup

but that did not work. I also tried with no luck:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | xargs -0 goiplookup

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2025-01-17 20:58:11

试试这个:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort | uniq | xargs -n 1 goiplookup
  • 我添加了 |排序| uniq 确保每个 IP 只出现一次
  • ,并且 xargs -n 1 以便每个找到的 IP 都由 goiplookup 处理

Try this:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort | uniq | xargs -n 1 goiplookup
  • I added | sort | uniq to ensure each IP only appears once
  • and xargs -n 1 so that each found IP is processes by goiplookup
云雾 2025-01-17 20:58:11

我会将其放入一个文件中,并创建一个小实用程序来解析它:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort -u > ./file.txt

cat ./file.txt | while read -r line; do
      temp$(echo $line)

      goiplookup $temp
done

这将一次一行读取文件,并对每个 IP 执行 goiplookup。

I would put it into a file and make a small utility to parse it:

sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}' | sort -u > ./file.txt

cat ./file.txt | while read -r line; do
      temp$(echo $line)

      goiplookup $temp
done

This will read through the file one line at a time and execute the goiplookup with each IP.

就是爱搞怪 2025-01-17 20:58:11
sudo grep disconnect /var/log/auth.log | awk '!/COMMAND/ && !seen[$0]++ {system("geoiplookup \""$9"\""}
  • 请注意,geoiplookup 每次调用仅允许一个 IP。

  • 整个事情可以在 awk 中完成,但使用 grep 允许其余部分在无特权的情况下运行。

  • 考虑一下 grep -w (匹配整个单词)是否合适,在 awk 中你可以用 !/(^|[^[:alnum:]_] 做类似的事情)命令($|[^[:alnum:]_])/.

sudo grep disconnect /var/log/auth.log | awk '!/COMMAND/ && !seen[$0]++ {system("geoiplookup \""$9"\""}
  • Note that geoiplookup only allows one IP per invocation.

  • The whole thing can be done in awk, but using grep allows the rest to be run unprivileged.

  • Consider whether grep -w (match whole word) is appropriate, and in awk you can do a similar thing with !/(^|[^[:alnum:]_])COMMAND($|[^[:alnum:]_])/.

过期情话 2025-01-17 20:58:11

我刚刚编写了一个 shell 脚本,可以运行。

#!/bin/bash
readarray -t array < <(sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}'  | sort | uniq)
for ip in "${array[@]}"
do
   :
 country=$(/usr/local/bin/goiplookup -c $ip)
 echo "$ip $country"
done

I just made a shell script, which works.

#!/bin/bash
readarray -t array < <(sudo grep "disconnect" /var/log/auth.log | grep -v COMMAND | awk '{print $9}'  | sort | uniq)
for ip in "${array[@]}"
do
   :
 country=$(/usr/local/bin/goiplookup -c $ip)
 echo "$ip $country"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文