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

sub Is_Host_Alive { my ($host) = @_; my $tmp_syn=Net::Ping->new("syn"); $tmp_syn->ping($host); if(!$tmp_syn->ack){ # print "$host unreachable \n"; $tmp_syn->close; return 0; } $tmp_syn->close; return 1; }

sometimes the running result is correct,but sometimes is wrong.

Replies are listed 'Best First'.
Re: Net::ping is very unstable
by Corion (Patriarch) on Apr 09, 2015 at 11:32 UTC

    In what way is it correct and in what way is it wrong?

    Note that you are checking whether SYN packets reach the host and not ICMP packets.

      the host is pingable by OS's "ping" command,but not pingable by this script.
      but some hosts in other net is pingable by this script.

        The ping command sends ICMP packets. Your code sends SYN packets and expects ACK replies.

        I recommend you consult with your network administrator how to best check the connectivity between hosts on your network.

        You can try to use Net::Ping::External, which actually invokes ping command.

        Creation of ICMP-packets requires special privileges on some systems (e.g. suid on linux), so, it is a bit not-convenient to use ICMP-protocol from perl. ping command already has that privilege/capability, and it is more-or-less safe to use it from security point of view.

        WBR, basiliscos.
Re: Net::ping is very unstable
by Perlbotics (Archbishop) on Apr 09, 2015 at 11:37 UTC

    Perhaps a firewall on the way to the host or the hosts own TCP stack configuration is interpreting your tests as a SYN-flood attack, consequently suppressing the ACK? What's the frequency of your tests?
    I had no problems with Net::Ping in the past.

    Update (in response to code block below): I second Corions advice given in Re^7: Net::ping is very unstable. (behold the use of [id://1122924] to point to that node ;-)

      #!/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?