Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script not counting D-DHCP

Posted on 2009-05-06
6
Medium Priority
?
286 Views
Last Modified: 2012-05-06
Hello,
I just noticed that the value reported on the log-external.txt shows that all results from DHCP are zereo.

See a sample output:
"169.66.213.209"        "XGVSSMS"       "PC"    "D-DHCP"        "trtsq.uk.steve.com"
"169.66.213.210"        "nint-cko8rs4"        "PC"    "D-DHCP"        "trtsq.uk.sarkie.com"
"169.66.213.211"        "SRSETSFTNDQ3J"        "PC"    "D-DHCP"        "trtsq.uk.sarkie.com"
"169.66.213.212"        "Richard-nherf647"       "PC"    "D-DHCP"        "trtsq.uk.richie.com"

From log-external.txt:

Subnet,Static,DHCP,Unused

10.0.0.0/8 ,37,0,217
146.149.0.0/16 ,174,0,39314
161.16.0.0/16 ,133,0,1671


I tried to change the %counts to D-DHCP as well as

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused};

to

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{D-DHCP}, $counts{Unused};

That just got me errors:

Bareword "D" not allowed while "strict subs" in use at fish3.pl line 18
Execution of fish3.pl aborted due to compilation errors.

#!perl
use strict; 
use warnings; 
use Data::Dumper; 
 
open my $log, '>', 'log-external.txt' or die "Could not open log: $!"; 
print $log "Subnet,Static,DHCP,Unused\n"; 
 
open my $dump, '>', 'dump.log' or die "failed to open 'dump.log' $!"; 
 
 
##### Step 1, read subnets 
open my $in, '<', 'm-names.txt' or die "Could not open m-names.txt: $!\n"; 
while( my $subnet = <$in>) { 
 
    print "Checking $subnet"; 
    chomp $subnet; 
     
    my %counts = (  
                   Static => 0, 
                   DHCP   => 0, 
                   Unused => 0, 
    ); 
     
    my @dnsoptions = `./getobjectlst.exe -u xx -p xx -o rich -a $subnet 2>&1`;
 
    print $dump "dumping \@dnsoptions\n";     
    print $dump Data::Dumper->Dump(\@dnsoptions); 
    print $dump '=' x 25, "\n"; 
 
    if ( @dnsoptions and $dnsoptions[0] eq '' ) {
        print "dnsoptions is null or undefined, going on to the next subnet\n"; 
        next; 
    } 
 
    # Now, at this point, we may have "Error 48" in @dnsoptions, or we may have 
    # the nicely formatted output. We'll have to check for both cases here. 
    # Let's check the unsuccessful case first. The condition below checks if 
    # the first element of @dnsoptions array has the following text in it - 
    # "Error 48: This subnet does not exist." in it. 
 
 
    if (join("",@dnsoptions) =~ /Error 48: This subnet does not exist./) { 
 
        # call "getsubnetlst.exe", passing $subnet as one of the parameters 
        my @subnetpart2 = `./getsubnetlst.exe -u xx-p xx -o rich -a $subnet -t network`; 
 
        print $dump "dumping \@subnetpart2\n";     
        print $dump Data::Dumper->Dump(\@subnetpart2); 
        print $dump '=' x 25, "\n"; 
 
         
        # now loop through each element of the array @subnetpart2, which looks like this - 
        # ########################################################################## 
        # "East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128" 
        # ########################################################################## 
        # pick up the 2nd field from the left (e.g. 146.149.1.0 above), and pass it as 
        # a parameter to the cli "getobjectlst.exe". 
 
 
        foreach my $line ( @subnetpart2 ) { 
 
            # get the 2nd field from the left 
            my $snetpart2 = (split/"\s+"/, $line)[1];
 
            # and now pass it to "getobjectlst.exe"; 
            my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -o rich -a $snetpart2`;
 
            print $dump "dumping \@dnsoptions2\n";     
            print $dump Data::Dumper->Dump(\@dnsoptions2); 
            print $dump '=' x 25, "\n"; 
             
            # find out counts of each subnettype (4th field from left) 
            foreach my $line (@dnsoptions2) { 
                my @subnettype = split/"\s+"/, $line; 
                $counts{$subnettype[3]}++; 
            } 
        } 
    } 
    else { # successful output from getobjectlst.exe 
 
        # find out counts of each subnettype (4th field from left) 
        foreach my $line (@dnsoptions) { 
            my @subnettype = split/"\s+"/, $line;
            $counts{$subnettype[3]}++; 
        } 
    } 
    printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused}; 
} 
 
close($in); 
close($log);

Open in new window

0
Comment
Question by:richsark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 24319527
Add quotes around D-DHCP
    my %counts = (  
                   Static   => 0, 
                   'D-DHCP' => 0, 
                   Unused   => 0, 
    ); 
     

Open in new window

0
 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 total points
ID: 24319558
You'll also need to used the quotes in the print statement.
0
 
LVL 1

Author Comment

by:richsark
ID: 24319828
Thanks FishMonger, will let you know
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:richsark
ID: 24324325
HI,

Man,... I tried everywhere to put quotes on this line, none are taking it.

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{D-DHCP}, $counts{Unused};


0
 
LVL 1

Author Comment

by:richsark
ID: 24324439
HI, I got it!,

I thought I tried this one already,

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{'D-DHCP'}, $counts{Unused};

Thanks
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 31578667
Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question