Perform an action if certain text exist in output (PERL)

Hello,

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
$nxdomain = "NXDOMAIN";
$ping = "100%";
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 and if 100% is in the output. It just goes ahead with the nslookup and then goes ahead with the ping.

Any suggestions?
Thanks!

When most of your "perl" is shell statements in backticks, you're fighting the language. Each individual set of backticks is its own independent shell anyway, you might as well use one shell instead of thirty.

#!/bin/sh

while read LINE
do
        # This works for Linux ping.
        # Solaris ping just prints 'x is alive' all by itself
        # others may vary.
        if ping -c 1 $LINE
        then
                echo "$LINE is alive"
        else
                echo "Couldn't ping $LINE"
        fi
done

I feel your pain, I learned Perl first too, but trying to use it as a shell just makes ugly code -- you'll spend most of your time, effort, and code getting things in and out of shell.

Strip out the perl, keep the shell, and you can do the same job faster in half the code.

You might find it better to process ping's output than nslookup's output, they both look up domain names.

I figured out a solution in Perl. it's a little sloppy but it gets the job done.

#!/usr/bin/perl
use warnings;
$seperator = "===============================================\n";
$ping = "100%";
open MYFILE, ">ips.txt" or die $!;
while (defined($line = <>)) {
	print "$line\n";
	$line =~ s/^\s+|\s+$//g;
	chomp($line);
	my $nslookupOut = `nslookup $line`;
	print $nslookupOut;
		if ($nslookupOut =~ /NXDOMAIN/) {
    my $pingOut = `ping -c 1 -w 1.0 $line`;
	print $pingOut;
    if ($pingOut =~ /$ping/) {
		print MYFILE "$line is available.\n"
		}
	}
}
close MYFILE;
print $seperator;
if (open MYFILE, "ips.txt") {
	chomp(my @lines = <MYFILE>);
	foreach (@lines) {
		print "$_\n";
		}
	}

Corona speaks the truth. If you're going to use a language, go all in and use if fully. I'm surrounded by system admins that write scripts in such a way. Better to adapt the good habits early on. Here's an example of what you're doing in pure Perl (with the help of a couple of modules):

#!/usr/bin/perl
#

use strict;
use Net::DNS;
use Net::Ping;

# create dns instance
my $ptr = Net::DNS::Resolver->new();

# create ping instance
my $do_ping = Net::Ping->new();

# prompt for ip address to lookup
print "IP Address -> ";
chomp(my $ip = <STDIN>);

# format ip address for reverse dns lookup
my $dns_suffix = ".in-addr.arpa";
my $reverse_ip = join('.', reverse(split /\./, $ip)) . $dns_suffix;

# perform dns lookup
my $response = $ptr->query($reverse_ip, 'PTR');
if($response) {
    print "PTR Record Found For $ip\n";
} else {
    print "PTR Not Found For $ip\n";
    print "Checking For Response From $ip\n";
    if($do_ping->ping($ip)) {
        print "$ip Is Responding To Pings\n";
    } else {
        print "$ip Is Not Responding To Pings\n";
    }
}

# done
exit(0);

At any rate, when something is out put, it is put out, and out means no longer in.

You can capture intermediate output in a variable up to about a megabyte, and query the inside of the variable, before finally putting it out.

dns=`nslookup $ip`