[H-GEN] testing network connectivity with perl

Conor Cunningham cunningtek at optusnet.com.au
Sun Aug 24 02:52:29 EDT 2003


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Hello,

I'm currently writing a script that simply pings a UNIX box every sixty 
seconds
and logs any failed pings. However, I have been asked to connect to an 
ORACLE port on the same UNIX box and log if the connection ever drops out.

The connection sometimes drops out, but only for a split second, and we 
need to prove this fact with logs files.

So to start off, I connect to the ORACLE port using IO::Socket and log 
success or failure.

I then enter a while(1) loop and do an

if(!connection) {log failure}

How I have been testing this is to simply unplug the network cable from 
my laptop, then replug it. The connection has been broken, but according 
to debugging, perl seems to think that the socket is still 
connected/functioning. This is proved by PERL entering an if statement 
which has a condition to enter only if the socket is "connected".

I am running the PERL script on a windows 2000 server box, with 
activestate PERL as the runtime environment. The UNIX box is some sun 
"mini" computer running SUN OS 5.8

My code is attaced below, and if anyone could help that would be great.

Regards,

Conor.

This is what I did;

N.B this is not all the code. The rest seems to work fine. I am using all 
required perl packages. The variable $rest is a user defined number as to how often they would like the program to check the connection.

#open permanant connection to oracle box

$oracle1 = IO::Socket::INET->new(
			PeerAddr => "nus807.in.telstra.com.au",
			PeerPort => 1521,
			Proto => "tcp",
			Timeout  => 1,
			Type => SOCK_STREAM ) or $error1 = 1;

if ($oracle1) {

print (LOG "Connection to ORACLE Box made at - $time\n");

}

else {

	print (LOG "Original Connection to ORACLE Box failed at - $time\n");

}





	
	

while (1) {

	if (!$oracle1) {
	
		print(LOG "Connection Broken to ORACLE box - $time\n");

                #testing
		print "connection failed.";

		#reopen connection
		$oracle1 = IO::Socket::INET->new(
			PeerAddr => "nus807.in.telstra.com.au",
			PeerPort => 1521,
			Proto => "tcp",
			Timeout  => 1,
			Type => SOCK_STREAM ) or $error1 = 1;
			
			#restore connection to oracle box
			print(LOG "Connection Restored to ORACLE box - $time\n");
	}

	if ($oracle1) {

                #testing
		print "connection successful";
	}

	$time = scalar(localtime());
	$x = 2;

	if ($p->ping($host)) {
		$x = 1;
		
	}

	if ($x!=1) 
	 {
		print (LOG "Connection is DOWN - $time\n");

	 }

	 #$open socket connection to nus807
         #another test to see if we can connect another ORACLE client to the server. Report success or failure
	$oracle = IO::Socket::INET->new(
			PeerAddr => "nus807.in.telstra.com.au",
			PeerPort => 1521,
			Proto => "tcp",
			Type => SOCK_STREAM ) or $error = 1;

	
	
	if ($error == 1) {
	
		print(LOG "Unable to connect to Oracle $time\n");
	}
	close($oracle);
	sleep($rest);

} #end  while



--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list