CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Perform an action if certain text exist in output


New User

Mar 7, 2013, 1:29 PM

Post #1 of 2 (488 views)
Perform an action if certain text exist in output Can't Post


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.

$nxdomain = "NXDOMAIN";
$ping = "100%";
while (defined($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?


Mar 8, 2013, 12:27 AM

Post #2 of 2 (478 views)
Re: [rudolphe] Perform an action if certain text exist in output [In reply to] Can't Post

I think you don't understand yet how an if statement works in Perl, and I strongly suggest that you read the documentation for this (you can get it by typing perldoc perlsyn on the command line).

In your case, you have

$nxdomain = "NXDOMAIN"; 
if ($nxdomain) {

which (since the variable $nxdomain never changes) boils down to:

if("NXDOMAIN") { 

In Perl, an expression is considered false if it is neither undef nor the null string ('') nor the string '0' nor numerically zero (0). Everything else is considered true. Since your expression consists of the string "NXDOMAIN", it is considered true and the block is always executed.

You probably want to search your line for the occurance of the string NXDOMAIN. There are several ways to do this. One is to used the index function. See perldoc -f index .

BTW, I strongly recommend that you put

use strict; 
use warnings;

on top of your code.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives