Solved

Script not counting D-DHCP

Posted on 2009-05-06
6
285 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

630 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