LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2007, 01:16 AM   #1
gubak
Member
 
Registered: Jul 2004
Posts: 332

Rep: Reputation: 30
Checking mail's content with a script


Hi!
I use CentOS 4.4, Postfix.
I want to perform some tasks (restarting the machine, starting and stoping ftp) from home. I don't want to use SSH, but I want to perform this tasks through mail system (I send a command and a program, which is running on the server, will check mails and execute the command).
For example I send a message to specific user on the server with the content: "service vsftpd start", and it will be executed.

My question is:
How can I check the mails' contents with a script?


Thanks!
 
Old 05-04-2007, 01:22 AM   #2
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
Code:
cat /var/spool/mail/<user>
 
Old 05-04-2007, 02:01 AM   #3
gubak
Member
 
Registered: Jul 2004
Posts: 332

Original Poster
Rep: Reputation: 30
This command print the entire mail. I just need subject.
How can I filter to print only the mail's subject?

Thanks!
 
Old 05-04-2007, 02:17 AM   #4
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
A basic way to get the subject would be:
Code:
cat /var/spool/mail/<user> | grep 'Subject:'
or
Code:
grep 'Subject:' /var/spool/mail/<user>
To strip "Subject:" from the start:
Code:
grep 'Subject:' /var/spool/mail/<user> | awk -F\: '{ print $2 }'
Someone else probably has a smarter way to do it using awk or similar...

Last edited by fukawi2; 05-04-2007 at 02:18 AM.
 
Old 05-04-2007, 02:51 AM   #5
gubak
Member
 
Registered: Jul 2004
Posts: 332

Original Poster
Rep: Reputation: 30
It's work, but how can I execute the command staying in the subject line?

Thanks
 
Old 05-04-2007, 07:28 AM   #6
gubak
Member
 
Registered: Jul 2004
Posts: 332

Original Poster
Rep: Reputation: 30
How can I print the result into the file?
 
Old 05-04-2007, 08:31 AM   #7
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
Code:
#!/bin/bash

#Find the commands that have been e-mailed
EMAIL_CMD=`grep 'Subject:' /var/spool/mail/<user> | awk -F\: '{ print $2 }' | head -n1`

#Execute the command
if [ ! -z "$EMAIL_CMD" ] ; then
    /bin/bash -c $EMAIL_CMD
fi

exit 0
UNTESTED. Could do some funky things - Check and test it before doing anything dangerous with it!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Howto install a content filtering and virus checking proxy (Part I) LXer Syndicated Linux News 0 04-12-2007 12:01 AM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 05:46 AM
read apache account mail's!! Dr_Death_UAE Linux - General 4 05-08-2006 06:23 AM
Protecting site content with java script Gogul Programming 15 02-28-2006 01:28 PM
use file content as tcl script parameters powah Linux - General 3 07-16-2005 12:10 PM


All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration