Operating System - HP-UX
1832308 Members
2138 Online
110041 Solutions
New Discussion

Checking Network Connectivity

 
Joseph A Benaiah_1
Regular Advisor

Checking Network Connectivity

I am writing a C program that issues a remsh command to a server, retrieves some data and processes it.

Is there any any way of checking the network connectivity using the system calls as remsh can take a lot longer to time out.

Regards,

Joseph.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Checking Network Connectivity

Hi Joseph:

Why not do a simple shell 'ping'?

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Checking Network Connectivity

Hi:

Unless you want to go to the trouble of setting up sockets, probably the easist method is to do a popen("ping remote_host -n 1","r")
and parse the results.

Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Checking Network Connectivity

Hi (again) Joesph:

You could verify connectivity with one 'ping' with this script. We look for a zero percent packet loss to confirm connectivity. The argument to the script constitutes an IPaddress or hostname:

#!/usr/bin/sh
typeset HOST=${1:-localhost}
ping $HOST -n 1|grep -q "0%"
if [ $? -eq 0 ]
then
echo "$HOST is alive"
else
echo "$HOST isn't responding"
fi
#.end.

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Checking Network Connectivity

Hi again Joseph:

Since you specifically mentioned C, I had a standard signal handler I use and I simply modified it for you specific needs. The timeout for ping can vary so this way you can set the timeout to any value you choose.

The function returns 0 is the ping is good and non-zero otherwise.

Clay
If it ain't broke, I can fix that.
Joseph A Benaiah_1
Regular Advisor

Re: Checking Network Connectivity

Thanks for such a quick response, I will probably have to set up a socket connection as I can ping a server, remsh, rexec, rlogin and telnet fails.

I will refer to my network programming book and see if there is a quick method for testing a remote connection.

If I was testing this from a shell, I would run:

if remsh ${host} "uptime" 2>&1 | grep -v -q "load average"
then
echo "Cannot remsh to ${host}"
exit
fi

Cheers,

Joseph.