in reply to Re^4: Net::ping is very unstable
in thread Net::ping is very unstable

If the Ping reply comes slowly, there is no way around that. If you want to ping more than one host, maybe you can parallelize the pings, for example using AnyEvent::Ping or Parallel::ForkManager or threads.

Replies are listed 'Best First'.
Re^6: Net::ping is very unstable
by hxbsy (Novice) on Apr 09, 2015 at 12:26 UTC
    #!/usr/bin/perl -w #use strict; use warnings; use IO::Handle; use Socket; use threads; use IO::Socket::INET; use Net::Ping; open(pctxt,'./pc.txt')||die "error open $!"; open(pc_log, ">./pclog.txt")||die "error open $!"; my $stime = time; while($read_data = <pctxt>) { #chomp; $read_data=~s/\s//g; $read_data=~s/" "//g; thread($read_data); } my $etime = time; my $diff = $etime-$stime; print "$diff \n"; close(pc_log); close(pctxt); ########################################## sub thread { my $net = shift; my @ip = (1..255); my $port = 12345; my $thr0 = threads->new(\&scan, $net,@ip[0..15]); my $thr1 = threads->new(\&scan, $net,@ip[16..31]); my $thr2 = threads->new(\&scan, $net,@ip[32..47]); my $thr3 = threads->new(\&scan, $net,@ip[48..63]); my $thr4 = threads->new(\&scan, $net,@ip[64..79]); my $thr5 = threads->new(\&scan, $net,@ip[80..95]); my $thr6 = threads->new(\&scan, $net,@ip[96..111]); my $thr7 = threads->new(\&scan, $net,@ip[112..127]); my $thr8 = threads->new(\&scan, $net,@ip[128..143]); my $thr9 = threads->new(\&scan, $net,@ip[144..159]); my $thr10 = threads->new(\&scan, $net,@ip[160..175]); my $thr11 = threads->new(\&scan, $net,@ip[176..191]); my $thr12 = threads->new(\&scan, $net,@ip[192..207]); my $thr13 = threads->new(\&scan, $net,@ip[208..223]); my $thr14 = threads->new(\&scan, $net,@ip[224..239]); my $thr15 = threads->new(\&scan, $net,@ip[240..254]); $thr0->join(); $thr1->join(); $thr2->join(); $thr3->join(); $thr4->join(); $thr5->join(); $thr6->join(); $thr7->join(); $thr8->join(); $thr9->join(); $thr10->join(); $thr11->join(); $thr12->join(); $thr13->join(); $thr14->join(); $thr15->join(); } sub scan { my ($net,@ip) = @_; foreach my $ip (@ip) { $host ="$net$ip"; if (Is_Host_Alive($host)) { my $socket = new IO::Socket::INET( PeerHost =>$host, PeerPort =>'12345', Proto =>'tcp' );# or die "ERROR in Socket Creation : $!\n"; if (($socket) && scan700($host)) { print "$host not shutdown \n"; } } # $socket->close(); } } sub scan700 { my ($ip) = @_; $host = "$ip"; my $socket = new IO::Socket::INET( PeerHost =>$host, PeerPort =>'700', Proto =>'tcp' );# or die "ERROR in Socket Creation : $!\n"; if($socket) { print "$host is ATM \n"; return 0; } #$socket->close(); return 1; } sub Is_Host_Alive { my ($host) = @_; my $tmp_icmp=Net::Ping->new('icmp'); if(!$tmp_icmp->ping($host)) { #print "$host ping unreachable \n"; $tmp_icmp->close; return 0; } $tmp_icmp->close; return 1; }

    "pc.txt" file as following:
    192.168.1.
    192.168.2.
    .....
    192.168.150.
    I want to check that whether so many net's hosts have been opened by port 12345 and not by port 700 or not.
    Can you give me better advice for the result?

      I've already linked you several approaches, but as you seem already have to have reviewed them and don't tell us how they are inappropriate to your task, let me link another one: Nmap.

        yeah,I'll try them.