Run remote command and send email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run remote command and send email
# 1  
Old 08-15-2017
Run remote command and send email

I am currently try to write a simple script with the following condition, but so far not having luck as the exit code would always be 0.
  1. Run remote command to read a file and grep for test word.
  2. if test word is in the file, send email.
  3. if not, do nothing
  4. also possible to ssh to multiple servers in one script?
Any help would be greatly appreciated. Thank you in advance!

Code:
#!/bin/bash
date=`/bin/date +%Y%m%d%H%M%S`

email_to="test_email@xxx.com"
email_subject="subject"
email_body="email body"

server="Host.google.com"

/usr/bin/ssh "$server" "cat log`date +"%m%d"` | grep test" >/dev/null 2>/dev/null

if [ $? -eq 0 ]

then
        echo "$email_body" | /bin/mailx -s "$email_subject" "$email_to"
        exit 1
fi

exit 0


Last edited by rbatte1; 08-16-2017 at 04:49 AM.. Reason: Converted textual numbered list to formatted numbered list and added ICODE tags for clarity.
# 2  
Old 08-15-2017
I can't confirm that "the exit code would always be 0" :
Code:
ssh root@"$server" "grep -q test log$(date +"%m%d")"; echo $?
0
ssh root@"$server" "grep -q txst log$(date +"%m%d")"; echo $?
1

Try
Code:
ssh root@"$server" "grep -q test log$(date +"%m%d")" && echo "$email_body" | /bin/mailx -s "$email_subject" "$email_to"
/bin/mailx -s subject test_email@xxx.com
email body

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-15-2017
I guess the problem is in your invocation of ssh:
Code:
/usr/bin/ssh "$server" "cat log`date +"%m%d"` | grep test" >/dev/null 2>/dev/null

The | grep test should really be running on your machine rather than the server. I also don't like the embedding of quotes - it might work, but ...

I've made a few changes to your code (untested).
Code:
#!/bin/bash
date=$(/bin/date +%Y%m%d%H%M%S)
# bash 4.2: printf -v mydate "%(%Y%m%d%H%M%S)T" -2
log_file=$(/bin/date +"log%m%d")
# bash 4.2: printf -v log_file "log%(%m%d)T" -2

email_to="test_email@xxx.com"
email_subject="subject"
email_body="email body"

server="Host.google.com"

/usr/bin/ssh "$server" "cat ${log_file}" | grep test >/dev/null 2>&1

if [ $? -eq 0 ]

then
        echo "$email_body" | /bin/mailx -s "$email_subject" "$email_to"
        exit 1
fi

exit 0

By setting the name of the log file outside of the invocation of ssh not only do you reduce the number of quotes in the line but it becomes easier to read.

Using backticks for process substitution is deprecated, I believe, and tends to be frowned upon in this forum; also $(...) is easier to read.

Is this any better?

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 4  
Old 08-16-2017
Thanks Andrew, learned something new today about backticks. Your modification did work as intended Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that will send an email if the cron job did not run.

Team, Would like to know what is the best script that will send you an email if cronjob did not run. Thanks (4 Replies)
Discussion started by: kenshinhimura
4 Replies

2. Shell Programming and Scripting

Until string from remote command equals value run remote command

I solved my issue by using the following code #!/bin/bash function GET_STATUS { #values Active Passive Failed ssh -a localhost '/home/user/fakecommand.sh' } STATE="unknown" until ] do echo $STATE sleep 5 STATUS=`GET_STATUS` echo $STATUS | grep Active &&... (1 Reply)
Discussion started by: $scipt_Kid
1 Replies

3. Shell Programming and Scripting

Send output of a command as an email

Hello Unix Experts! We are on AIX 6.1 TL6 I am trying to develop a script that does the following: It should send the output of "df -g /directory/folder" command as an email to a user(someone@company.com) This is too simple and i can research on how to do it, but it would be great if... (2 Replies)
Discussion started by: atechcorp
2 Replies

4. UNIX Desktop Questions & Answers

Send Email from command line in Ubuntu 11.10

Have problem to send email from command line according to the posts like this one: To have the ability to send email from the command line, you will need to install the mailutils and postfix packages with the following commands. apt-get install mailutils apt-get install postfix Now... (1 Reply)
Discussion started by: yifangt
1 Replies

5. Shell Programming and Scripting

Script to Send an email using mail command

Hello all I'm trying to write a script to send an email, so I can be nnotified when something is going wrong with my system. I've tried many options with no luck: mail -s "You've got mail" somebody@example.com echo "Mail Body" | mail -s "Subject" somebody@example.com I tried also to... (1 Reply)
Discussion started by: samer.odeh
1 Replies

6. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

7. UNIX for Dummies Questions & Answers

Not able to send email as CC to a ID using Mailx command in HP UX

Hey Guys, I have been trying to send a email as ccto a email ID .However it seems the -c option is not working in HP UX system. It is shooting an error as "mailx: illegal option -- c" My script isin the below manner: mailx -c EMAIL_IDS_CC# -s 'BDC reminder' -r sender@email.com... (1 Reply)
Discussion started by: rohit.shetty84
1 Replies

8. Shell Programming and Scripting

script to send command periodically to remote server

Hi, I'm wondering if there's a way to send a command periodically to remote server through a script. Right now I have this: keepLooping=1 ssh user@domain while (( keepLooping == 1 )) do echo a sleep 3 done but what this does is ssh to the server, and only when the connection is... (2 Replies)
Discussion started by: sayeo
2 Replies

9. Programming

How do I send email from command prompt?

hi all How do I send email from command prompt? i tried this # mutt -s "Test mail" -a /root/Desktop/email1.txt XXXXX@yahoo.co.in < /root/Desktop/email.txt Error sending message, child exited 71 (Operating system error.). Segmentation fault # and also root@localhost ~]# /bin/mail -s "what... (0 Replies)
Discussion started by: munna_dude
0 Replies

10. UNIX for Dummies Questions & Answers

Shell to run query, encrypt and send email

Hello To do : 'automate the process of generating report and send it to client every 15 days after encrypting the report file' Did so far : - generate Java program to encrypt the file. - generate SQL query to run the report. Is there any body who can help me to go further ? ... (7 Replies)
Discussion started by: cub
7 Replies
Login or Register to Ask a Question