Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Non-empty strings treated as null?!!

by watkin (Initiate)
on Feb 12, 2014 at 23:35 UTC ( [id://1074709]=perlquestion: print w/replies, xml ) Need Help??

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

Dear perlmonks,

As a novice perl programmer, could someone help me solve this seemingly impossible apparent error with my perl interpreter (running on my somewhat dated Linux Mint 14 Nadia system). (I am trying to ban FORUM SPAMMERS from posting to a Drupal content-managed web-site I help administer.)

Essentially, the code, listed below erroneously interprets two clearly non-null strings as being null.

#!/usr/bin/perl use Net::Nslookup; my ($name, $ipAddr); foreach (@ARGV) { $name = nslookup(host => $_, type => "PTR"); if($name != "") { print("looking up ... $name ..."); $ipAddr = nslookup(host => $_, type => "A"); } else { print("NOT looking up ... $name ..."); } print "$_ $name $ipAddr\n"; }

The command-line I use is:

nsl.pl 109.163.234.7 136.169.192.25 174.140.166.22 178.137.180.11 188. +126.75.245 204.45.103.68

The output is:

NOT looking up ... edwardsnowden0.torservers.net ...109.163.234.7 edwa +rdsnowden0.torservers.net NOT looking up ... ...136.169.192.25 NOT looking up ... ...174.140.166.22 looking up ... 178-137-180-11-broadband.kyivstar.net ...178.137.180.11 + 178-137-180-11-broadband.kyivstar.net NOT looking up ... c-188-126-75-245.anonymous.at.anonine.com ...188.12 +6.75.245 c-188-126-75-245.anonymous.at.anonine.com NOT looking up ... ...204.45.103.68

Reverse (PTR) lookups of 109.163.234.7 and 188.126.75.245 give me respectively edwardsnowden0.torservers.net and 178-137-180-11-broadband.kyivstar.net , which are clearly NON-NULL strings, yet the test in my perl program indictes that those strings are NULL !?

Can someone suggets to me how I can fix this apparent problem with perl? I truly need a more efficent way of querying these IP address than with the linux nslookup command-lien utility.

Thank you for your attention,

watkin

Original content restored above by GrandFather

Dear perlmonks,

Thank you, tangent.

I'll try your cleaned up version and let you know how it goes.

... and, thank you, davido.

The 'eq' operator in place of '!=' has fixed the problem. I'll be sure to include the use warnings; statement in my perl scripts from now on.

I need to quickly test the whole C class IP address range.

Whilst use of the perl script will be more efficient than the nslookup linux utility program, what I really need is a means to quickly test the whole IP C class network containing the host (e.g. 136.169.192.25) from which the forum spam was posted (i.e. all IP addresses from 136.169.192.0 to 136.169.192.255) I would like to be able to quickly tell whether or not that network is known to the global DNS/Bind database. If it is not, I can quickly add the whole network (indicated by '136.169.192.%' with the wild '%'character) to the drupal/Mysql 'access' table and set the "access type" field to 'deny'.

Havingto test each of the 256 IP addresses in a C Class range would take a a lot of efrort even with the perl script

Replies are listed 'Best First'.
Re: Non-empty strings treated as null?!!
by davido (Cardinal) on Feb 12, 2014 at 23:49 UTC

    What happens when you switch if( $name != "" ) to if( $name ne "" )?... or perhaps if( length $name )

    This issue would have been caught if your script started with use warnings; When doing relational comparisons with strings, you should use the stringy numeric operators: eq, ne, gt, ge, lt, or le. If you treat string like a number (ie, use numeric relational operators such as ==, !=, <, <=, >, >=, the empty string evaluates to numeric zero.


    Dave

Re: Non-empty strings treated as null?!!
by tangent (Parson) on Feb 13, 2014 at 00:03 UTC
    '!=' is for numbers, 'ne' is for strings, but in this case you don't need to use either as nslookup() returns undefined if it fails. Here's a little cleaned up version:
    use strict; use warnings; use Net::Nslookup; foreach (@ARGV) { my $name = nslookup(host => $_, type => "PTR"); if ($name) { print("looking up ... $name ..."); my $ipAddr = nslookup(host => $_, type => "A"); print "$_ $name $ipAddr\n"; } else { print("NOT looking up ... $_ ...\n"); } }
Re: Non-empty strings treated as null?!!
by tweetiepooh (Hermit) on Feb 13, 2014 at 12:26 UTC
    Just being pedantic but .0 is the network address and .255 the broadcast so you don't need to test these.
    On that note, if you need to block a whole range can the product not take a network address, so adding 136.169.192.0 would block the whole range? (Or something similar).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1074709]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found