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. | [reply] [Watch: Dir/Any] [d/l] |
|
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?
| [reply] [Watch: Dir/Any] |
|
I'd have to see your updated code.
| [reply] [Watch: Dir/Any] |
|
|
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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. | [reply] [Watch: Dir/Any] |
|
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).
| [reply] [Watch: Dir/Any] |
|
What Perl installation do you have on your Windows box? If you're using ActiveState, do the following to install a module:
- Open a DOS prompt.
- Change directories to the location where you installed Perl.
- Change into the bin subdirectory of your Perl installation.
- 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
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Array Problem
by ysth (Canon) on Jul 08, 2005 at 14:37 UTC
|
$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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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) {
}
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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.
| [reply] [Watch: Dir/Any] |
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 :)
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
use strict;
use warnings;
do? I added it and it stopped the program from completing execution? | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
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!
| [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
That is because I forgot ;
$baseIP = "10.0.5.";
At the begining of the program.
Good Luck
| [reply] [Watch: Dir/Any] [d/l] |
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;"
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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?
| [reply] [Watch: Dir/Any] [d/l] [select] |
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! | [reply] [Watch: Dir/Any] |
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. | [reply] [Watch: Dir/Any] |