Solved

ping and catching output

Posted on 1997-09-15
10
386 Views
Last Modified: 2012-05-04
Hi,

I've got the following problem.

I'm working under an Windows 95 environment and I'm writing perl scripts
using the cgi.pm module.

I want to use a ping command to check if my mail server is on line, I've
already tried the pingecho function in the ping.pm module but i always
get the next error message : Error parse exception

This is the test script i wrote that generated the error message :

use lib 'c:\perl\lib';
use Net::Ping;

$ip_adres = "192.92.130.6";
$timeout = 10;

print "yes\n" if pingecho($ip_adres, $timeout);

What could this error message mean ?

I also tried to use the ping command that comes with windows.
but the problem with this is, i don't know how to check if the server is
online or not. The yes is always printed out!

perl script for this :

$ip_adres = "192.92.130.6";
$ping="ping";

open(PING, "|$ping $ip_adres");
close(PING);
if (!$?) { print "yes" };

I'm just a beginner, please help me
0
Comment
Question by:madness082997
  • 5
  • 4
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1206569
As I commented in
http://www.experts-exchange.com/topics/bin/ShowQ?qid=8630017924
berore it turned into a Server Error,
Have you tried:

open(PING,"$ping $ip_adres|");
@pingresult=<PING>;
close(PING);
print @pingresult;

or

use Net::Ping;
$p = Net::Ping->new("tcp", $timeout);
print "$host not reachable "  unless $p->ping($host);
print "$host is alive.\n" if pingecho($host);

0
 

Author Comment

by:madness082997
ID: 1206570
Hi,

The first part :

open(PING,"$ping $ip_adres|");
@pingresult=<PING>;
close(PING);
print @pingresult;

works but the second part :

use Net::Ping;
$p = Net::Ping->new("tcp", $timeout);
print "$host not reachable " unless $p->ping($host);
print "$host is alive.\n" if pingecho($host);

just gives the same error message.

The first part worked, but i still don't know how to check if @pingresult contains the word 'timed out'wich indicates the host is down.

Any idea's ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206571
grep(index($_,'timedout')>=0,@pingresult);

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 84

Expert Comment

by:ozo
ID: 1206572
or

{local $/=undef; $pingresult=<PING>;}
print "timedout\n" if( index($pingresult,'timedout')>=0 );


0
 

Author Comment

by:madness082997
ID: 1206573
Ozo,

could you explain that last comment ?

Thank you
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206574
Ok, you asked how you could check if the array @pingresult contained
the word 'timedout', so I gave one way to check using grep,
and as an alternative I gave a way to read the result into a scalar $pingresult
and to check if that scalar contained the word 'timedout'
Does that explain what you needed?

 
0
 

Author Comment

by:madness082997
ID: 1206575
Yes it does !

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206576
So do you think you have the Answer you were asking for, or
are there furthur questions?
(By the way, your old question on this same topic has now reapeared (with a different title)
You may wish to delete it so as not to be spending your points on duplicates)

0
 

Author Comment

by:madness082997
ID: 1206577
this is the answer i was looking for !

Thanks again
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 90 total points
ID: 1206578
$ip = "192.92.130.6";
$ping = "ping";   # probably you must use a full pathname here
open(PING, "$ping $ip |") || die "Error: $!\n";
while(<PING>) {
  m/^reply\s/i   && do { print "yes"; exit(0); };
  m/^request\s/i && do { print "no";  exit(1); };
}
close(P);
exit(2);

this works for NT, even with M$'s perl :-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question