Script to connect to remote and sendmail.

Hello,

Kindly guide.

SendMail function on my script is not working, but it works manually.
Any better way to handle the script is appreciable.


#!/bin/sh
GetHostConnection()
{
truncate --size 0 /home/web/for_mail.out
while read -r lines ;
do
        ip=`echo $lines | awk '{print $1}'`
        port=`echo $lines | awk '{print $2}'`
        ssh -q -n -o "BatchMode=yes" -p $port $ip "echo 2>&1"
        if [ $? -eq 0 ];
        then
        echo "Connected to ${ip} on port ${port}" > /dev/null
        else
        echo "Connection refused to ${ip} on port ${port}" >> /home/web/for_mail.out
        fi
done < /home/web/ssh_configfile
}

PushFileToMail_IP()
{
scp /home/web/for_mail.out user@1.2.3.183:/xyz/INVESTIG/Sadiq/ 2>/dev/null
}

SendMail()
{
ssh user@1.2.3.183
cd /xyz/INVESTIG/Sadiq
SUBJECT="ALERT: Source IP Connection Refused"
BODY="Dear Team,\n\nKindly find the below host and their port which are unable to connect.\n \n `cat /xyz/INVESTIG/Sadiq/for_mail.out` \n .
Please verfied and take a necessary action with concerned team."
printf "${BODY}"| mailx -s "$SUBJECT" abc@xyz.com
exit
}

for a server, I am checking the host connection which i can only check for this dedicated server.
I am printing it into an output file called "for_mail.out".
next step I am scp the file to remote server from where i need to send mail connecting remotely.

Kindly help.

Regards.
sadique

For a start, it might help if you had matched double-quotes on the line:

BODY="Dear Team,\n\nKindly find the below host and their port which are unable to connect.\n \n `cat /xyz/INVESTIG/Sadiq/for_mail.out` \n .

And, it always helps if you tell us:

  1. what operating system you're using,
  2. what shell you're using,
  3. and show us (in CODE tags) all of the diagnostic messages produced by your code when you run it (whether sent to stdout, stderr, or /home/web/for_mail.out ).
1 Like

what operating system you're using?
Linux
what shell you're using
bash

SendMail()
{
ssh user@1.2.3.183
cd /xyz/INVESTIG/Sadiq
SUBJECT="ALERT: Source IP Connection Refused"
BODY="Dear Team,\n\nKindly find the below host and their port which are unable to connect.\n \n `cat /xyz/INVESTIG/Sadiq/for_mail.out` \n .
Please verfied and take a necessary action with concerned team."
printf "${BODY}"| mailx -s "$SUBJECT" abc@xyz.com
exit
}

Above code is not working, I am unable to sendmail.
Manually when i do ssh it connect to the server and the cd to the path and can able to send mail.
But when i apply the same on my Sendmail function its not doing the same.

Kindly guide and let me know if anymore things required.

Regards,
Sadique

As you may infer from the lack of reactions, more info is needed. You might want to

  • set the -x option before calling the function
  • print out your variables for debug purposes
    and post the results so people in here have something to work on. "Above code is not working" doesn't really help nailing down the problem.

you might want to consider something along these lines - not tested...:

SendMail()
{
ssh -q user@1.2.3.183 "\
    cd /xyz/INVESTIG/Sadiq \
    SUBJECT='ALERT: Source IP Connection Refused' \
    BODY='Dear Team,\n\nKindly find the below host and their port which are unable to connect.\n \n `cat /xyz/INVESTIG/Sadiq \ /for_mail.out` \n . \
Please verfied and take a necessary action with concerned team.' \
     printf \"${BODY}\"| mailx -s \"$SUBJECT\" abc@xyz.com" </dev/null
}
1 Like

Hi vgersh99,
Note that command substitution (neither $(command) nor `command` ) does not work inside single-quotes. And, you have an extraneous <backslash> character after ... concerned team.' .

Hi sadique.manzar,
I also note that in post #3 you still haven't fixed the mismatched double-quotes that I mentioned in post #2.

And, although you have now told us what OS and shell you're using, you still haven't shown us the output produced when you run your script.

2 Likes

Dear vgersh99,
On your logic my mail is working but, it doesn't contain the subject and body.

Here is the output:
cat: /xyz/INVESTIG/Sadiq/: No such file or directory
cat: /for_mail.out: No such file or directory
Null message body; hope that's ok

As Cragun mentioned single quote / double quote both are not reading subject and body on mail.

Kindly guide.

Does /xyz/INVESTIG/Sadiq/ exist on the remote node?
Is PushFileToMail_IP() executed? Correctly? You redirected its stderr to /dev/null .

@Don Cragun: I don't see a problem with the BODY variable assignment continued in the next line.

1 Like

Thanks Cragun, files was existing at remote place, I have removed the PushFileToMail_IP() as inside ssh i can access the local files.
Fixed the issue.

Now i am able to get the required output.

Dear Team,

Kindly find the below host and their port which are unable to connect.

Connection refused to abc@1.2.3.42 on port 2244

Connection refused to abc@1.2.3.43 on port 2244

Please verified and take a necessary action with concerned team.

Regards,
Sadique

I didn't look closely enough at the code that vgersh99 suggested in post #5. The command substitutions are inside single quotes inside double quotes. Inside double quotes, the single quotes are just regular characters, so there is no reason that the command substitution should not work as long as the file that is the operand to cat is on the local machine; not on the remote machine.

What wasn't clear from your code (and may be what the problem is in vgersh99's code) is whether the file /xyz/INVESTIG/Sadiq is supposed to be found on the machine on which you are running your script or on the machine to which you are ssh ing.

I apologize for any confusion I caused in post #6.