+ Reply to Thread
Results 1 to 2 of 2

Thread: Perl : Ping a HOST

  1. #1
    mike
    Guest

    Perl : Ping a HOST

    How can i ping a host usiing PERL ?
    Last edited by mike; 11-12-2012 at 02:51 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    Here is the snippet i am using to Ping a Remote Host :
    Code:
    # subroutine: ping_machine
    # accepts: hostname/ip address, timeout(ms), # of echos to send
    # returns an array:
    #	0:	status: DNS_FAIL/PING_FAIL/PING_PARTIALLY_OKAY/PING_OKAY
    #	1:	ip address of the host (v4)
    #	2:	min delay (ms)
    #	3:	max delay (ms)
    #	4:	ave delay (ms)
    # calls the windows ping utility
    
    sub ping_machine {
    
    	my @return_array;
    
    	my $status;
    
    	my $ip_address;
    
    	my $max;
    
    	my $min;
    
    	my $ave;
    
    	my $host = @_[0];
    
    	my $timeout = @_[1];
    
    	my $packets = @_[2];
    
    	open(PING,"ping $host -w $timeout -4 -n $packets |");
    
    	while(<PING>) {
    
    		if(/could not find/) {
    
    			$status = "DNS_FAIL";
    
    		}
    
    		if(/\[([0-9\.]+)\]/) {
    
    			$ip_address = $1;
    
    		}
    
    		if(/Packets\: Sent \= ([0-9]+)\, Received \= ([0-9]+)\, Lost \= ([0-9]+)/) {
    
    			if($1 eq $2) {
    
    				$status = "PING_OKAY";
    
    			} else {
    
    				if($1 eq $3) {
    
    					$status = "PING_FAIL";
    
    				} else {
    
    					$status = "PING_PARTIALLY_OKAY";
    
    				}
    
    			}
    
    		}
    
    		if(/Minimum \= ([0-9]+)ms\, Maximum \= ([0-9]+)ms\, Average \= ([0-9]+)/) {
    
    			$min = $1;
    
    			$max = $2;
    
    			$ave = $3;
    
    		}
    
    	}
    
    	$return_array[0] = $status;
    
    	$return_array[1] = $ip_address;
    
    	$return_array[2] = $min;
    
    	$return_array[3] = $max;
    
    	$return_array[4] = $ave;
    
    	return @return_array;
    
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts