将 bash 数组中的新条目追加到 txt 文件

发布于 2024-10-02 19:59:06 字数 1099 浏览 5 评论 0原文

情况是这样的,我编写了一个小脚本来生成电子邮件被拒绝的 IP 地址列表:

msgid_array=($(grep ' sendmail\[' /var/log/maillog |  
               egrep -v 'stat=(Sent|queued|Please try again later)' |  
               egrep dsn='5\.[0-9]\.[0-9]' | awk '{print $6}'))  

for z in ${msgid_array[@]}; do  
    ip_array[x++]=$(grep $z /var/log/maillog | egrep -m 1 -o 'relay=.*' |  
                    egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')  
done

所以它所做的就是查找被拒绝电子邮件的所有消息 ID 并将它们存储在 msgid_array 中。

然后使用 for 循环 grep 包含每个消息 ID 的邮件日志,过滤出发件人 IP 地址并将所有 IP 存储在 ip_array 中。

现在我打算每天运行它并让它解析昨天的日志条目,然后将结果存储在单独的 txt 文件中。

如果我的 txt 文件中有一个“rejected_ip_addresses=”条目,我怎样才能简单地将任何新的 IP 地址添加到现有列表中?

所以今天我运行它,条目看起来像这样:

rejected_ip_adresses=1.1.1.1 2.2.2.2

明天当我运行它时,数组看起来像这样,因为同样的 2 个发件人在发送电子邮件时遇到问题,但有 2 个新的问题:

ip_array=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4)

所以我在 txt 中的条目现在应该看起来像这样像这样,重点是每月概述所有有问题的地址:

rejected_ip_adresses=1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4

感谢您的任何想法,目前我的大脑拒绝帮助我。

Here's the situation, I wrote a small script to generate the list of IP addresses from which an e-mail was rejected:

msgid_array=($(grep ' sendmail\[' /var/log/maillog |  
               egrep -v 'stat=(Sent|queued|Please try again later)' |  
               egrep dsn='5\.[0-9]\.[0-9]' | awk '{print $6}'))  

for z in ${msgid_array[@]}; do  
    ip_array[x++]=$(grep $z /var/log/maillog | egrep -m 1 -o 'relay=.*' |  
                    egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')  
done

So what it does is it looks for all the message ID's of the rejected e-mails and stores them in the msgid_array.

Then using the for loop it grep's the maillog with each message ID and filters out the senders IP address and stores all the IP's in the ip_array.

Now I intend to run this each day and let it parse the log entries for yesterday then store the results in a separate txt file.

If I have a "rejected_ip_addresses=" entry in my txt file, how could I simply just add any new IP addresses to the existing list?

So today I run it and the entry looks like this:

rejected_ip_adresses=1.1.1.1 2.2.2.2

Tomorrow when I run it the array looks like this because the same 2 senders had problems with sending e-mail but there are 2 new ones:

ip_array=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4)

So my entry in the txt should now look like this, the point being having a monthly overview of all the problematic addresses:

rejected_ip_adresses=1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4

Thanks for any ideas, currently my brain is refusing to help me.

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

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

发布评论

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

评论(2

相思碎 2024-10-09 19:59:06

我会将条目(每行一个)附加到文件中并执行 sort -u

printf "%s\n" ${ip_array[@]} >> problem_ips.txt
sort problem_ips.txt > tmp.txt && mv tmp.txt problem_ips.txt

您可以通过将循环替换为以下内容来显着加快速度:

ip_array=($(printf "%s\n" ${msgid_array[@]} | grep -f - /var/log/maillog ... ))

您还可以通过替换多个来稍微提高速度调用 grep 并调用 awk 执行相同的操作。然而,最大的收获将是删除所有这些 grep 被多次调用的循环。

I would append the entries, one per line, to the file and do a sort -u:

printf "%s\n" ${ip_array[@]} >> problem_ips.txt
sort problem_ips.txt > tmp.txt && mv tmp.txt problem_ips.txt

You can speed things up considerably by replacing your loop with:

ip_array=($(printf "%s\n" ${msgid_array[@]} | grep -f - /var/log/maillog ... ))

You might also get a slight speed increase by replacing multiple calls to grep with one call to awk that does the same operations. However, the biggest gain will be in removing that loop where all those greps are called many times.

香橙ぽ 2024-10-09 19:59:06

也许您可以拥有名称和值对(我的意思是变量),您可以将其源到脚本中。同样,在退出脚本之前,您可以再次重新创建名称和值对。

Example:

Variables.conf
  rejected_ip_adresses=1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4
  [email protected]
  [email protected]

parse.sh
  source variable.conf
  .
  . 
  .
  <parsing logic>
  .
  .
  .
  .
  <loop to store the variables back>

probably you can have name and value pairs(I mean variables) which you can source into your script. Again before you exit the script you can recreate the name and value pair again.

Example:

Variables.conf
  rejected_ip_adresses=1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4
  [email protected]
  [email protected]

parse.sh
  source variable.conf
  .
  . 
  .
  <parsing logic>
  .
  .
  .
  .
  <loop to store the variables back>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文