Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Array Problem

by wanderinweezard (Initiate)
on Jul 08, 2005 at 14:19 UTC ( [id://473420]=perlquestion: print w/replies, xml ) Need Help??

wanderinweezard has asked for the wisdom of the Perl Monks concerning the following question:

I'm very new to perl and I am writing a simple utility to ping our company's network. I have the perl script execute the OS ping command and output it to a file. I then read this file and determine if the ping was a success or not. The problem I am having is that when I go to write the results to an array for storage, I don't think the values are actually getting written. Here is the code I have written:
$baseIP = "10.0.5."; $j = 1; until ($j == 255) { $string = $baseIP.$j; $input = "-w 50 -n 1 ".$string." |find /V \"Received\" |find /V \"P +ing\" |find /V \"A\""; system("ping $input >> ping.txt"); $j++; print "$j\n"; } open(RESULTS2, "ping.txt") || die; open(RESULTS, ">ping.txt") || die; @results = <RESULTS2>; close(RESULTS2); $j = 1; $a = 0; $f = 0; while (@results) { if (m/Reply/) { $output[$j] = $baseIP.$j.": Alive\n"; $j++; $a++; } elsif (m/Request/) { $output[$j] = $baseIP.$j.": Dead\n"; $j++; $f++; } else { } } print RESULTS "Pinger Results:\n\n"; print RESULTS "Alive Nodes: $a\n"; print RESULTS "Dead Nodes: $f\n"; print RESULTS "\n\nDetailed Results:\n"; print RESULTS @output; close(RESULTS);
When I examine the results in the ping.txt, I find Alive Nodes is zero, Dead nodes is zero, and there aren't any detailed results... Does anyone see my problem right out?

Replies are listed 'Best First'.
Re: Array Problem
by Transient (Hermit) on Jul 08, 2005 at 14:29 UTC
    open(RESULTS2, "ping.txt") || die; open(RESULTS, ">ping.txt") || die;
    You open a file for reading and then you truncate that file before reading from it. Try moving the write after reading (and closing) the first filehandle - or try writing to a different file.
      I did tr that, now I get an error of:

      Use of uninitialized value in pattern match (m//) at pinger.pl line 22, <RESULTS2> line 1016.

      Any idea what could cause that?
        I'd have to see your updated code.
Re: Array Problem
by davidrw (Prior) on Jul 08, 2005 at 14:33 UTC
    initial reaction is that Net::Ping maybe be helpful to avoid the external creation of ping.txt.. Also, i would suggest adding use strict; and use warnings; ... to debug your problem of 0 alive and 0 dead, i would put a debugging print statement in the while(@results){} loop to see what it's doing.. it's probably not matching on /Reply/ or Request so first step is to look at what string it's searching.. can you show us a snippet from ping.txt?

    quick side note: foreach my $j ( 1 .. 255 ){} is a more "Perl-ish" way to write that first loop.
      Thanks for the perl style advice, I'll make sure to change that. Here is a snippet of the ping.txt. I am also looking into Net::Ping, but am not sure how to install it in a Win32 environment.
      
      
      Reply from 10.0.5.1: bytes=32 time<1ms TTL=255
      
      
      
      
      Request timed out.
      
      
      
      
      Reply from 10.0.5.3: bytes=32 time<1ms TTL=128
      
      
      
      
      Reply from 10.0.5.4: bytes=32 time<1ms TTL=128
      
      
      
      
      Reply from 10.0.5.5: bytes=32 time<1ms TTL=255
      
      
      
      
      Reply from 10.0.5.6: bytes=32 time<1ms TTL=128
      
      
      Yes, those are spaced like that in the actual file.
        There's good stuff in the Tutorials on installing modules (and specifically in Win32 with ppm)

        the spaces should be fine with those regex checks you had.. after seeing the other posts i too think it was the cloberring of ping.txt that made it appear to have no data.. A debugging print statement in there by the regex check may still be a good idea (if in there orginally, it would have pointed in the direction of the clobbering because you'd see that the print wasn't being reached).

        What Perl installation do you have on your Windows box? If you're using ActiveState, do the following to install a module:

        1. Open a DOS prompt.
        2. Change directories to the location where you installed Perl.
        3. Change into the bin subdirectory of your Perl installation.
        4. Type ppm at the prompt.

        This runs the ppm.bat batch file and opens an interactive PPM session. (You can tell you're in a PPM session because the prompt is ppm>.) Type help to get a listing of commands. In the case of Net::Ping, type:

        search Net-Ping

        and press Enter. PPM will search ActiveState repositories for the module and return a numbered listing of its findings. For example:

        ppm> search Net-Ping Searching in Active Repositories 1. Net-Ping-External [0.11] Cross-platform interface to ICMP "ping" +utilities ppm>

        To install one of the listed modules, simply type the install command along with the name, or number, of the desired package, like so:

        ppm> install 1 or ppm> install Net-Ping-External

        PPM will fetch and install the module and write a series of messages to the screen. When it completes, it returns to the PPM prompt. Type q to quit the PPM session.

        PPM will make your module-installing life much easier under Windows. :)

        I hope this helps.

        /Larry

Re: Array Problem
by ysth (Canon) on Jul 08, 2005 at 14:37 UTC
    Other comments:
    $j = 1 until ($j == 255) { ... $j++; ... }
    This will loop from 1 to 254, perhaps not what you want. Alternatives:
    $j = 1; while ($j < 256) { ... }
    or
    for my $j (1..256) { }
    Also consider Net-Ping.
      Thanks for your ideas, but I really do want it to just do 1 through 254, but I do like the the
      for my $j (1..256) { }
      Just curious about the logic behind this correction..

      Given that the network address (.0) and the broadcast address (.255) is rarely used for hosts (assuming last digit of a Class C network) then one would rarely want to ping the .255 address.

        Well, I was going to say something about the evils of until/unless, but was really too tired to be coherent, so didn't end up saying all I meant. In other words, questioning the bounds was just an excuse to question the structure.
Re: Array Problem
by rev_1318 (Chaplain) on Jul 08, 2005 at 14:37 UTC
    Since you open ping.txt for reading and immediately for writing, you globber the file. That's why is empty (apart from the statistics lines you put in).
    because of that, @results will be an empty list, which saves you from the next problem: the infinite loop in while (@results) {...}

    To loop over all elements of result, use:

    foreach (@results) { ... }
    Use different files for reading and writing or read the file first, then reopen it for writing.

    Lastly, put

    use strict; use warnings;
    on top off your programs :)

    Paul

      What does
      use strict; use warnings;
      do? I added it and it stopped the program from completing execution?

        Check out the tutorial: Use strict warnings and diagnostics or die - it should answer your question. Be sure to read the replies too.

        "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: Array Problem
by sapnac (Beadle) on Jul 08, 2005 at 15:05 UTC
    Hi! Check this code. I've assumed the results are in ping.txt and are valid. This is tested and working...
    open(RESULTS2, "ping.txt") || die; open(RESULTS, ">ping2.txt") || die; *********** someother file ******* +******* @results = <RESULTS2>; close(RESULTS2); $j = 0; *************************array's initial subscript $a = 0; $f = 0; foreach (@results) { ************** URs went into a loop* if (m/Reply/) { $output[$j] = $baseIP.$j.": Alive\n"; print $output[$j]."\n"; $j++; } elsif (m/Request/) { $output[$j] = $baseIP.$j.": Dead\n"; print $output[$j]."\n"; $j++; } } print RESULTS "Pinger Results:\n\n"; print RESULTS "Alive Nodes: $a\n"; print RESULTS "Dead Nodes: $f\n"; print RESULTS "\n\nDetailed Results:\n"; print RESULTS @output; ************************************************** Hope this helps. Thanks!
      That's great, it seems to do what I want now.. But for some reason in the part where it prints the @output in the ping2.txt file, it does not include $baseIP, but looking at the code, I'm not sure I see anything wrong.
        That is because I forgot ; $baseIP = "10.0.5."; At the begining of the program. Good Luck
Re: Array Problem
by Necos (Friar) on Jul 08, 2005 at 16:45 UTC
    Well, I wrote a similar test program for my linux machine to test the status of our network printers. It's a simple snippet of code.
    #!/usr/bin/perl -w use strict; my $ip = '10.195.16'; open( 'FH' , ">printer_ping"); for ( 30 .. 200 ) { my $test = join('.',$ip,$_); print "Now pinging $test\n"; my $output = `ping -c 2 $test`; print $output; print FH $output; }
    The nice thing about this code is that it prints both to standard output, and to file, and can be easily modified to print anywhere else.
    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
      That's great, it works really well. I'm trying to simplify the output into basically IP Address: Result... So, I was wanting to, using your example, pass $output through a check and see if it contains either the word "Reply" or the word "Request"... Is it possible to do something like this?

      I am not sure how to make the (m/Reply/) check if that is inside the $output variable?
Re: Array Problem
by cajun (Chaplain) on Jul 10, 2005 at 05:54 UTC
    Here's another post that might help you Pingger Script4. Be sure to read the replies!
Re: Array Problem
by wanderinweezard (Initiate) on Jul 11, 2005 at 16:09 UTC
    Thanks everyone for all your help. I was able to get this running how I wanted thanks to you all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://473420]
Approved by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found