mail script to automate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mail script to automate
# 1  
Old 09-07-2009
mail script to automate

Hi,

Here below the logs from the mail server:

less /var/log/messages:

Sep 6 04:03:31 server-59 out[30914]: 1252227811|webmaster@zilia.com|antonino.granata@gmail.com|2175|success|1
Sep 6 04:03:33 server-59 in[30897]: 1252227813|news@tarot.com|junk@thess.com|30376|success|1
Sep 6 04:03:35 server-59 in[30932]: 1252227815|mtarmizi@perak.gov.my|baha.ansari@risates.com.pk|2934|success|1
Sep 6 04:03:47 server-59 in[30933]: 1252227827|admin@joboffer.com|kemera@mol.com.mk|1736|success|1

Where:

1252227827 -> this is one of the id generated for every second. Using this id we can find out how many email where send out with in a second.
I 'll use the below command to find out the count of emails send out per account:

less /var/log/messages/ | awk '{print $1,$2,$3, +$6}' | grep "Sep 6" | sort | uniq -c | sort -n

Here below the output of the above command:

Output:
40 Sep 6 06:31:02 1252236662
36 Sep 6 21:07:39 1252289259
19 Sep 6 06:31:24 1252236684
17 Sep 6 12:14:22 1252257262
17 Sep 6 05:25:57 1252232757
13 Sep 6 10:51:03 1252252263

Then,

less /var/log/messages | grep 1252236662

It will list the count of 40 mails send out from this id:

Sep 6 06:31:02 server-59 in[27341]: 1252236662|afnaz.bv@sysh.com.sa|achupnm@gmail.com|8035635|success|1
Sep 6 06:31:02 server-59 in[27341]: 1252236662|afnaz.bv@sysh.com.sa|achupnm@yahoo.co.in|8035635|success|1
Sep 6 06:31:02 server-59 in[27341]: 1252236662|afnaz.bv@sysh.com.sa|afsal.ambalavan@alsaif-motors.com|8035635|success|1
Sep 6 06:31:02 server-59 in[27341]: 1252236662|afnaz.bv@sysh.com.sa|afsalpioneer@gmail.com|8035635|success|1
Sep 6 06:31:02 server-59 in[27341]: 1252236662|afnaz.bv@sysh.com.sa|akthabkm@gmail.com|8035635|success|1

So, I need to automate the process of the above one. while running the command it should email me the email account . For eg: the account "afnaz.bv@sysh.com.sa" has used 40 in count. I need a script to email me the top 15 counts and the email account. Is this possible Via the shell script.
# 2  
Old 09-07-2009
Code:
sed -n '/^Sep 6/s/[^ ]* [^ ]* [^ ]* [^ ]* [^ ]* [^|]*|\([^|]*\)|.*/\1/p' yourLogFile |\
sort | uniq -c | sort -n | head -15 |\
mail -s 'mail count' yourID@your.dom

or shorter:
Code:
awk -F'|' '/Sep 6/ {print $2}' yourLogFile |sort |uniq -c |sort -n |tail -15 |mail -s 'mail count' youID@you.dom

The mail will look like this:

40 afnaz.bv@sysh.com.sa
31 news@tarot.com
...

Hope some awker can do it straight.

Last edited by daPeach; 09-07-2009 at 12:09 PM.. Reason: code cleaning
# 3  
Old 09-07-2009
Yes. It looks like you have most of the pieces in place already. (Though why do you use less instead of cat?)

You can use cron to run you script at intervals that you decide and the output stdout and stderr will be emailed to the address you provide.

I always keep a text file, ~/text/crontab.txt, to simplify updating cron. I edit it and then call this command to update what cron does.
Code:
crontab ~/text/crontab.txt

Here is a sample file. Be sure to specify the path, because cron executes with a very limited environment.
Code:
# crontab for Ken Jackson
# Time fields:  minute, hour, day, month, day-of-week (0 or 7 is Sun)
MAILTO=ken
22  3  * * *  $HOME/bin/cron_attempt_remote
4   4  * * *  $HOME/bin/cron_uptime
58  6  * * *  $HOME/bin/fixhtaccess -q
6   9  * * *  $HOME/bin/fixhtaccess -q

# 4  
Old 09-07-2009
In the sed command, the path for the file is not mentioned, eg., /var/log/messages and also, this is an automated process, which should will be running in an hourly basis. So, that we can't specifically check with the date, like sep 6.

I hope it makes sense.
# 5  
Old 09-07-2009
Could this fit your needs:
Code:
theDate=$(LC_TIME=C date +'%b %d %H')
sed '/'$theDate'/s/[^ ]* [^ ]* [^ ]* [^ ]* [^ ]* [^|]*|\([^|]*\)|.*/\1/' /var/log/messages |\
...

# 6  
Old 09-08-2009
While running the scripts as a .sh, it gives the error message as

sed: -e expression #1, char 4: unterminated address regex
Null message body; hope that's ok
# 7  
Old 09-09-2009
I tired with different options, but still getting with the same error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

automate the script

Dear all, I I want to login to my Linux machine using putty and then run some script from Windows machine.we can do it after loging it and then execute the script by typing it in putty command line screen. but I want to automate it.So whenever I will fire this script,it will do the following... (4 Replies)
Discussion started by: smartgupta
4 Replies

2. Shell Programming and Scripting

Automate remote script

Hi all, I need to execute a script on a remote machine that are connected to the network.The basic requirement is to write a script which will login in remote machine and then execute the other script automatically placed in remote machine.So that I need to execute the remote machine script... (3 Replies)
Discussion started by: smartgupta
3 Replies

3. Shell Programming and Scripting

How can I automate a script?

Hi All, Can I automate a script when some one trying to 'vi' (open) a file. For Example, I am having a file named 'SecuredShell.sh'. when a user types " vi SecuredShell.sh " in unix command prompt a script named secure.sh needs to be automated. Can this be possible. if Yes please guide... (2 Replies)
Discussion started by: little_wonder
2 Replies

4. Shell Programming and Scripting

Automate Script ***V. Urgent

Hi All, ./procdure.ksh which opens the below the menu, I want to build a script which will press 4 and run the Sector Data Automatically (instead of pressing option 4 manually) Is there any way for this, please let me know... 1) FX Rates MDU 9) Fidessa Cash... (7 Replies)
Discussion started by: niceboykunal123
7 Replies

5. UNIX for Dummies Questions & Answers

how to automate incoming mail processing

Hi All, I require to develop some script which will continuously be looking for mails from some specific mail addresses on AIX server. Once any such mail arrives, the process will look into the mail subject and mail body to search for some keywords like success or failure, filename etc.... (3 Replies)
Discussion started by: vivek8220
3 Replies

6. Shell Programming and Scripting

Automate shell script

I would like to automate script where i do not have to manually insert the username and password I wrote two different scripts but not able to achieve the results: here's to scripts i wrote #!/bin/bash cd /var/tmp /home/server/steve/pca --askauth -idx /opt/app/bin/expect <<EOF expect... (3 Replies)
Discussion started by: sam786
3 Replies

7. UNIX for Advanced & Expert Users

Shell Script to Automate

I would like to automate script where i do not have to manually insert the username and password I wrote two different scripts but not able to achieve the results: here's to scripts i wrote #!/bin/bash cd /var/tmp /home/server/steve/pca --askauth -idx /opt/app/bin/expect <<EOF expect... (1 Reply)
Discussion started by: sam786
1 Replies

8. Shell Programming and Scripting

Script(s) to Automate Tasks

I know that this has a bad title, but I'm not sure how to sum it up well. (And I'm new to Linux in general, so please excuse any incorrect terminology :o) Anyway, I'm trying to create a script that gets whatever folder you're in, and then does a command with that directory as an argument. I... (2 Replies)
Discussion started by: droppedonjapan
2 Replies

9. Shell Programming and Scripting

here document to automate perl script that call script

I am trying to use a here document to automate testing a perl script however when the perl script hits a system(perl subscript.pl) call, input is no longer entered into this subscript. here is my script $ cat test.sh #ksh for testcase do program <<-EOF | tee -a funcscnlog.log y... (3 Replies)
Discussion started by: hogger84
3 Replies

10. UNIX for Dummies Questions & Answers

automate the input in a script

I have a program that i have to run by cron. The program needs user input. So i have to automate that in a littke script. start of script program.sh: result=program.log; export result echo Program starting : `date` >> $result /usr/local/program >> $result echo Program running : `date` >>... (11 Replies)
Discussion started by: erwinspeybroeck
11 Replies
Login or Register to Ask a Question