Script to connect to remote and sendmail.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to connect to remote and sendmail.
# 1  
Old 02-21-2018
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.

Code:
#!/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
# 2  
Old 02-21-2018
For a start, it might help if you had matched double-quotes on the line:
Code:
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).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-21-2018
what operating system you're using?
Linux
what shell you're using
bash
Code:
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
# 4  
Old 02-21-2018
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.
# 5  
Old 02-21-2018
you might want to consider something along these lines - not tested...:
Code:
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
}

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 02-21-2018
Quote:
Originally Posted by vgersh99
you might want to consider something along these lines - not tested...:
Code:
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
}

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.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-22-2018
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. HP-UX

Connect to remote server using sftp with password define within command/script

I am trying to connect to remote server in hp-ux, using sftp command (using sftp username@ip and password ) able to connect to remote server but, in this case sftp prompt for password and user need to manually enter it. I want sftp can read a password define in script or from file, so it can... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. Shell Programming and Scripting

Bash script connect to remote servers and become root

Hi, I need a script that will connect to a list of servers and first sudo to root and then run a couple of commands. For security reasons, we can't setup ssh keys as root. Manually I have to login to a server as user and then sudo to root. It's not possible to use root@servername , because of... (8 Replies)
Discussion started by: misterx12345
8 Replies

4. AIX

AIX Remote Connect Fail With “No more multiple IP addresses to connect” Error

We have a production server at a client site running AIX. And recently when users are trying to connect to it via telnet, it prompts "No more multiple IP addresses to connect". Can I know what does this error mean? and how to rectify this? Thanks. (2 Replies)
Discussion started by: a_sim
2 Replies

5. Shell Programming and Scripting

Script to connect to a remote server and execute scripts

Hello All I need a script or set of commands which can establish a remote connection with another server and execute some scripts over there. Basically it has to establish the connection with the remote server as an user ,say 'testuser' and then execute the script 'testscript'. and return the... (5 Replies)
Discussion started by: sgbhat
5 Replies

6. Shell Programming and Scripting

shell script for how to connect to a remote server by using ssh

i want to connect to a remote server through ssh. i have to also provide password within that script. after connecting to the remote server i want to do some operations like grep,cd etc can u pls help me to wite a script. Thanks (1 Reply)
Discussion started by: millan
1 Replies

7. Shell Programming and Scripting

How can my unix shell script automatically connect to remote host?

How can my unix shell script automatically connect to remote host? Assume that there is a remote host called "rhost". When I connect to that host i give the command "telnet rhost". It then asks me for my id and password. Once i give it connects there. I want to automate these steps. I want... (8 Replies)
Discussion started by: digdarshan
8 Replies

8. Shell Programming and Scripting

Connect to a Remote Sybase Server Through Script

Hi all, I am trying to connect to Remote Sybase database Server through shell script. I am operating on WindowsXP, connect to Unix(version SunOS: 5.8) The thing is i dont know how to connect to Sybase Server through my script file? Are there any manual pages which can guide me through the... (3 Replies)
Discussion started by: Aparna_k82
3 Replies

9. Windows & DOS: Issues & Discussions

Connect to a Remote Sybase Server Through Script.

Hi all, I am trying to connect to Remote Sybase database Server through shell script. I am operating on WindowsXP, connect to Unix(version SunOS: 5.8) The thing is i dont know how to connect to Sybase Server through my script file? Are there any manual pages which can guide me through the... (2 Replies)
Discussion started by: Aparna_k82
2 Replies

10. UNIX for Advanced & Expert Users

sendmail : connect local host (brocken)

hi there I've got a strange problem with a webserver box (suse linux 7.2). My MTA is sendmail, and when I tried manually send a mail. Sendmail -v "user@something" < /etc/hosts The Server tried "simon@localhost... Connecting to local..." but nothing happend and the system hold on. This... (3 Replies)
Discussion started by: mrsaint
3 Replies
Login or Register to Ask a Question