Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if connection is up, if not send email to...

Status
Not open for further replies.

blueeyedme

Technical User
Nov 27, 2002
39
NL
Hi !

At work we have a seperate isdn connection to a remote server.
Every once in a while this isdn router goes down. What i would like to do is make a script that checks if ip adres : 172.25.20.2 is active (its a ftp connection) and if not it sends me a email that the connection is down.
When i ftp to 172.25.20.2, it makes a isdn connection true that router and from there it goes to the remote server over the isdn connection.

So a script that checks if this connection is up? anyone ?

Thanks !
 
something along these lines:
#--------------------------------------
#!/bin/ksh
MyUser='blueeyedme'
MyPassword='heresMyPassword'
MyEmailAddress='blueeyedme@myCompany.com'

ftp -n -v <<EOF
open 172.25.20.2
user ${MyUser} ${MyPassword}
ls
EOF

if [ $? -ne 0 ] ; then
mailx -s 'FTP connection DOWN' ${MyEmailAddress} < $(date)
fi; vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks vlad !!

can you tell me what 'if [ $? -ne 0 ] ;' exactly does ?

thanks !!!!!!!!
 
$? has the value of the 'return status' of the LAST ran command. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,

I am getting the following error after running it.

With the set -x command in the script i see it logging in and out and then at the end it says :

syntax error at line 14: `(' unexpected


Does this sound familiar to you ?

Jeroen
 
ooops.
change

mailx -s 'FTP connection DOWN' ${MyEmailAddress} < $(date)

TO

mailx -s &quot;$(date) - FTP connection DOWN&quot; ${MyEmailAddress} < /dev/null

make sure you're running this as ksh vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,

I think the script works, the only thing is it doesnt send the email when its down. (tried this by entering a incorrect ip adres)

thanks !

jeroen
 
why not just ping it first?

if [ -z &quot;`ping 172.25.20.2 5|grep alive`&quot; ]
then
# the 5 is a five second timeout(increase or decrease to your liking)
mailx .......
else
# it's alive so let's proceed with the ftp
ftp.....
fi
 
Well the script works.. only the email part doesnt.

I can see it log in and out.. and if i use a incorrect ip adress it gives a error no connection.

 
your mail/sendmail might be miss-configured.
try sending mail [with mailx] manually from the command line - see if it works. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Try redirecting exec and exec2 to log with the script running set -vx and have a look at the execlog to see if there's anything obvious.

#! /bin/ksh

set -vx
exec >$EXECLOG
exec 2>$EXECLOG
EXECLOG=/tmp/execlog$$
SERVER=172.25.20.2
TOUT=5
USER=username
PASS=password
FILE=filetosend
MAILX=/usr/bin/mailx
GREP=/usr/bin/grep
PING/usr/sbin/ping
FTP=/usr/bin/ftp
MYMAIL=mail@address
DATESTAMP=`date +%d-%m-%Y&quot; &quot;%H:%M:%S`
SUBJECT=&quot;connection to '$SERVER' is down: $DATESTAMP&quot;


if [ -n &quot;`$PING $SERVER $TOUT|$GREP alive`&quot; ]
then
echo &quot;
user $USER $PASS
put $FILE
bye&quot;|$FTP -n $SERVER
else
echo &quot;\nfailed to reach '$SERVER'\n&quot; >/dev/tty
echo &quot;$SUBJECT\n&quot;|$MAILX -s &quot;ftp failure&quot; $MYMAIL
exit 1
fi
 
You'll have to set EXECLOG before you try redirecting anything to it though. ;-) Annihilannic.
 
This is my script so far :


set -x
#!/bin/ksh
MyUser='user'
MyPassword='pass'
MyEmailAddress='name@host'

ftp -n -v <<EOF
open 10.0.5.10
user ${MyUser} ${MyPassword}
ls
EOF

if [ $? -ne 0 ] ; then
mailx -s &quot;FTP connection DOWN&quot; $MyEmailAddress < /dev/null
fi;


The error i get when the connection is down :

Connection timeout

The thing is that it doesnt send me the email then. IT DOES send me a email when i use mailx at the prompt as following :


mailx -s &quot;FTP connection DOWN&quot; user@host < /dev/null


Can anybody help me ?

thanks,

Jeroen

 
ftp apparently doesn't return an exit status.

Try redirecting the output of the FTP command by using this line:

[tt]ftp -nv > /tmp/ftp.out.$$ 2>&1 << EOF[/tt]

And change your if statement to read:

[tt]if grep ^230 /tmp/ftp.out.$$
then
continue
else
mailx -s &quot;FTP connection DOWN&quot; $MyEmailAddress < /dev/null
fi[/tt]
rm /tmp/ftp.out.$$

230 is the code returned by FTP for a successful login. Annihilannic.
 
\u@\h[\w]> ./mk_ftpcheck.sh
MyUser=user
MyPassword=passwd
MyEmailAddress=user@host
MyLog=/var/opt/oracle/bene/log/ftp.out.$$ 2>&1
+ ftp -n -v
open 10.0.5.10
user user passwd
ls
+ grep
+ 230 /var/opt/oracle/bene/log/ftp.out.401
./mk_ftpcheck.sh: 230: not found
Usage: grep -hblcnsviw pattern file . . .
+ mailx -s FTP connection DOWN user@host

The script doesnt work anymore now. No matter if the site is up or down it keeps mailing me. With grep it says as above (./mk_ftpcheck.sh: 230: not found Usage: grep -hblcnsviw pattern file . . . )

Anybody ?
thanks !

 
Its working now ! thanks everyone..

I had the logfile wrong. works WHOOHOOOO :)
 
Good news!

Under sh you have to surround &quot;^230&quot; with quotes. For some reason the character '^' has special meaning to sh, but I can't see anything that says what that meaning is! Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top