0

I'm attempting to write a tool that checks an IP address for existing PTR records then if there are no PTR records does a ping to see if it response.

Then if there is no response, it should print a message saying

This is what I have so far.

#!/usr/bin/perl
$ping = "100%";
use warnings;
while (defined($line = <>)) {
    chomp($line);
    print `nslookup $line`;
    if ("NXDOMAIN") {
        print `ping -c 1 -w 2.0 $line`;
        if ($ping) {
            print "IP Address '$line' is available.\n"
        }
    }
}

What I can't seem to get to work are the if statements for if the text NXDOMAIN is in the output it performs the ping. Then if the text of the ping contains $ping it prints the following line.

print "IP Address '$line' is available.\n"

Any assistance would be greatly appreciated. Thanks!

1

3 Answers 3

1

You should use the built in libraries of perl to do what you need. Here is an example of how to achieve your ping with perl:

use Net::Ping;

my @list = ("10.0.0.1", "10.0.0.2", "10.0.0.3");

foreach my $addr (@list) {
    my $p = Net::Ping->new();
    print "IP Address '$addr' is not responsive.\n" if !$p->ping ($addr);
    $p->close();
    }
0

You need to store the value returned by your commands:

# THIS CODE IS UNTESTED, but should get the idea across.
my $nslookupOut = `nslookup $line`;
print $nslookupOut;
if ( $nslookupOut =~ /NXDOMAIN/ ) {
    my $pingOut = `ping -c 1 -w 2.0 $line`;
    print $pingOut;
    if ( $pingOut =~ /$ping/ ) {
        ...
    }
}
1
  • That's exactly what I needed. That's going to go a long way towards me getting good at Perl. Thanks!! Mar 8, 2013 at 15:11
0

$ping isn't undef. So the if condition always returns true!

You need to set the $ping variable if you get negative response from that particular IP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.