Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

User input compared with generated list

by zuinc (Novice)
on Apr 03, 2002 at 17:51 UTC ( #156366=perlquestion: print w/ replies, xml ) Need Help??
zuinc has asked for the wisdom of the Perl Monks concerning the following question:

Im new at perl and this script is in its infacy. I am writing a script where the user is prompted to enter an IP address. From the address entered, the program will determine its subnet and compare it with a file containing a list of all the subnets on the network. The PROBLEM is that I cant get the script to check the list and compare the stored subnet address with the list. The troubled area is in red. Im pretty sure I am doing wrong in regards to accessing the list. Here is my code:
#/usr/local/bin/perl # Creates a list of all subnets $sn = 0xffff & system("ypcat hosts | cut -f1-3 -d \".\" | sort -u | so +rt -k 1n,1 n -k 2n,2n -k 3n,3n -t \".\" -r>subnets.list"); # User is prompted to anter an IP address open(CHECK, ">check.list") || die "ERROR! This file cannot be created! +\n"; print "Enter an IP address.\n"; $ip = <STDIN>; chomp ($ip); $partial = rindex($ip, "."); $ip_subnet = substr($ip, 0, $partial); chomp ($ip_subnet); print CHECK "$ip_subnet\n"; # Checkpoint - prints subnet # Match entered subnet to active subnet list open(SUBNET, "subnets.list") || die "Unable to open file: subnets.list +\n"; while (<SUBNET> ) { if ($ip_subnet eq $subnet) { print "PASS\n"; } else { print "FAIL\n"; } }
So, for example, if the user enters 12.34.56.78, the program will store 12.34.56 as the subnet and then should compare 12.34.56 with the list subnets.list and return either a true or false value. Please HELP! Thanks. Zuinc

Comment on User input compared with generated list
Download Code
Re: User input compared with generated list
by PrimeLord (Pilgrim) on Apr 03, 2002 at 18:09 UTC
    Try this.
    while (<SUBNET> ) { chomp; if ($ip_subnet eq $_) { print "PASS\n"; } else { print "FAIL\n"; } }


    Update: I realized it would probably be better to also explain what might have been wrong. When you read the subnet.list file into the SUBNET file handle each line gets written into $_ not $subnet. The (<SUBNET>) is just a shortcut for ($_ = <SUBNET>). Also I added the chomp statement to remove any newline characters that would throw off your match. Again using just chomp; is shorthand for chomp $_;.

    HTH

    -Prime
      I tried your suggestion even before and it didnt work. For some reason, the program returns fail equal to the number of entries found in the file containing the list of subnets. For example, if there are 100 subnets, the output would be 'FAIL' 120 times. Thanks for the advice thus far. Any other suggestions? ~Zuinc
Re: User input compared with generated list
by RMGir (Prior) on Apr 03, 2002 at 18:23 UTC
    I'm not sure your 2nd loop does what you intend.

    Your code compares $ip_subnet with EVERY entry in the subnets.list file, and gives PASS/FAIL for each.

    I think what you want is a PASS if the entry appears anywhere in the list, and fail otherwise.

    There's at least 2 ways to do this, one that's good if you're going to have to check a lot of $ip_subnet values against the same stable list, and one for checking just one value, or checking values against a changing list.

    # in your initialization... my %subnets; open(SUBNET, "<subnets.list") or die "Couldn't open subnets.list for reading, error $!\n"; while(<SUBNET>) { chomp; $subnets{$_}=1; } close(SUBNET); # for each read $ip_subnet: if(exists $subnets{$ip_subnet}) { print "PASS\n"; } else { print "FAIL\n"; }
    That way, you only read in subnets.list once, and you can reuse it in your code.

    If the file is expected to change, or you only need it once, then you'd have to do something like this:

    open(SUBNET, "<subnets.list") or die "Couldn't open subnets.list for reading, error $!\n"; my $exists=0; while(<SUBNET>) { chomp; if($_ eq $ip_subnet) { $exists++; last; } } close(SUBNET); if($exists) { print "PASS\n"; } else { print "FAIL\n"; }
    Hope this helps, and that I understood your problem correctly...
    --
    Mike
      Awesome! It actually did print PASS the first time but I wasnt able to see it amongst all those FAILS. But I only wanted a PASS or FAIL value printed once and I was guided in the right direction. Thanks everyone. ~Zuinc
        Glad to help!

        By the way, stephen is right; don't forget -w and use strict. They'll save you much grief in the long run.
        --
        Mike

Re: User input compared with generated list
by stephen (Priest) on Apr 03, 2002 at 18:33 UTC

    You might want to check on Net::NIS. This module allows you to access the 'ypmatch' functionality directly, without having to do all that sorting and cutting. (Haven't used it myself, but the docs look promising.)

    You'll also definitely want to use strict; and put a -w on your Perl invocation line. :)

    stephen

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-09-17 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (71 votes), past polls