For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Open files and looking a word









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Open files and looking a word
Rodrigo Tavares

2007-08-31, 7:27 pm

Hello,

I'm doing a script, it run a ping in many computers
and create a log file.

I need search in log file the word "Destination Host
Unreachable". So, I have to take below line. After
using regular expressions taking ferrari.lab and print
"The host is down !".

example:
PING ferrari.lab (192.168.1.152) 56(84) bytes of data.
From goal.lab (192.168.1.7) icmp_seq=2 Destination
Host Unreachable

How I do it ?

Begin of script, wih array with host names:

print "Testing the computers....\n";
for (my $i=0 ; $i < 5 ; $i++)
{
system "ping -c 4 $hosts[$i] >> hosts.txt";
}


Best regards,

Faria


Flickr agora em português. Você clica, todo mundo vê.
http://www.flickr.com.br/
Chas Owens

2007-08-31, 7:27 pm

On 8/31/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
> Hello,
>
> I'm doing a script, it run a ping in many computers
> and create a log file.
>
> I need search in log file the word "Destination Host
> Unreachable". So, I have to take below line. After
> using regular expressions taking ferrari.lab and print
> "The host is down !".
>
> example:
> PING ferrari.lab (192.168.1.152) 56(84) bytes of data.
> From goal.lab (192.168.1.7) icmp_seq=2 Destination
> Host Unreachable
>
> How I do it ?
>
> Begin of script, wih array with host names:
>
> print "Testing the computers....\n";
> for (my $i=0 ; $i < 5 ; $i++)
> {
> system "ping -c 4 $hosts[$i] >> hosts.txt";
> }
>


Snip if you have an array of hosts then you should say

for my $host (@host) {
system "ping -c 4 $hosts >> hosts.txt";
}

instead, but really, you shouldn't even be using system:

#!/usr/bin/perl

use strict;
use warnings;

use Net::Ping;

my @hosts = @ARGV;
my $pinger = Net::Ping->new;

for my $host (@host) {
print "$host is down\n" unless $pinger->ping($host);
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com