Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

A better way to test network elements

by tradez (Pilgrim)
on Mar 07, 2002 at 18:34 UTC ( #150080=perlquestion: print w/replies, xml ) Need Help??
tradez has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,
  &nsbp I am sure that there is a better way to do what I am trying to do. I am writing a script that allows people to input an IP from our call center and do simple tests. Forward and Reverse NS Lookup, Ping, and the killer, Traceroute. Pretty simple stuff, but the traceroute is killing me. I am on sunOS 5.7, and the traceroute binary I have running does not seem to have a way to die. Basically, if someone puts in a bad a ip, ie it will get to the gateway and just hang. Currently I work around this by
$fork = fork(); if(fork){ sleep 20; open (MYFILE, "traceroute$time.txt"); @data = <MYFILE>; if (!@data){ $error = 'Ip could not be Tracerouted'; push (@lines, $error); }else{ foreach $line (@data) { $line =~ s/\n/<br>/g; push (@lines, $line); } } }else{ $tracert = `traceroute "$ip" > traceroute$time.txt`; }
A simple, yet archaic answer to an issue that I know can be solved more easily. I was thinking about just using a system() call, sleeping, then checking it to see if I got results, but that would really not speed things up much. Please help me to find a better way!

"Never underestimate the predicability of stupidity"
- Bullet Tooth Tony, Snatch (2001)

Replies are listed 'Best First'.
Re: A better way to test network elements
by Moonie (Friar) on Mar 07, 2002 at 18:45 UTC
Re: A better way to test network elements
by ajwans (Scribe) on Mar 07, 2002 at 21:34 UTC
    Forking does not seem to be the optimal solution to this. I would prefer to set a handler for SIGALRM and set an alarm, something like.

    $SIG{ALRM} = sub { die ("Couldn't traceroute"); }; alarm (20); if ($tracert = `traceroute "$ip"`) { alarm (0); } # handle data from traceroute here

    This is documented in the perlipc perldoc.

    1. dude, what does mine say?
    2. "sweet", what about mine?
    3. "dude", what does mine say?
    4. GOTO 2

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://150080]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-01-19 20:08 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (222 votes). Check out past polls.