Solved

Script not counting D-DHCP

Posted on 2009-05-06
6
279 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
  • 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 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now