开发者

Append new entries from bash array to a txt file

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.


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.


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
  from_email=test@test.com
  to_email=test@test.com

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜