大佬教程收集整理的这篇文章主要介绍了检查文件的每个 ip 是否存在于另一个文件中,然后采取行动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有 ipList.txt 包含我想禁止的 ip 列表(一行一行,从 file.log 中提取并对应于特定条件,例如 3 个或更多访问权限); 我想检查 ipList.txt 的每个 ip 是否已经存在于 iptables 中,或者是否存在于 file.dump 中(我从 iptables-save > file.dump 导出的 iptables):
@H_696_4@ipList.txt
123.123.123.123
234.234.234.234
222.111.222.111
...
file.dump
-A input -s 123.123.123.123/32 -j DROP
-A input -s 111.222.111.222/32 -j DROP
-A input -s 234.234.234.234/32 -j DROP
-A input -s 126.127.126.127/32 -j DROP
...
以 30 秒循环检查的 bash 脚本:
@H_696_4@#!/bin/bash
while [ 1 = 1 ]
do
# checking the presence or not of each ip I want to ban from ipList.txt,in file.dump
ip2ban=$(cat ipList.txt)
for i in $ip2ban
do
ipcheck=$(egrep -o "$ip2ban" file.dump)
if [ -z "$ipcheck" ]
# if $ip2ban is not present in file.dump
then
echo "$ip2ban is NOT in file.dump file then I BAN IT!"
# command to ban via iptables here
sleep 1
else
# if $ip2ban is already present in file.dump
echo "$ip2ban is in file.dump then I do nothing and conTinue!"
sleep 1
fi
sleep 30
done
done
exit
它不起作用,因为它似乎一次检查整个 ipList.txt 包含的内容,而不是前一行之后的每一行!?
@H_696_4@I got this:
123.123.123.123
234.234.234.234
222.111.222.111 is in file.dump then I do nothing and conTinue!
but I should got that:
123.123.123.123 is in file.dump then I do nothing and conTinue!
234.234.234.234 is in file.dump then I do nothing and conTinue!
222.111.222.111 is NOT in file.dump file then I BAN IT!
在线:
@H_696_4@ipcheck=$(egrep -o "$ip2ban" file.dump)
你有egrep -o "$ip2ban"
。而 ip2ban
包含您的整个文件:ip2ban=$(cat iplist.txt)
您需要将行 ipcheck=$(egrep -o "$ip2ban" file.dump)
更改为:
ipcheck=$(egrep -o "$i" file.dump)
以上是大佬教程为你收集整理的检查文件的每个 ip 是否存在于另一个文件中,然后采取行动全部内容,希望文章能够帮你解决检查文件的每个 ip 是否存在于另一个文件中,然后采取行动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。