Link to home
Start Free TrialLog in
Avatar of richsark
richsarkFlag for United States of America

asked on

Enhanced count script wrapper

I have this a code that was created here, I need someones expertise  to modify it slightly and I am not sure where to start or yet fully understand the logic.


Right now the code works like this: Reference post :
https://www.experts-exchange.com/questions/24349421/count-script-wrapper.html?anchorAnswerId=24249912#a24249912

 I am trying to get a script to read my m-names.txt which has lots of
167.0.0.0/8
168.0.0.0/8
192.0.0.0/8

I need to strip the / part and only use the subnet address:
167.0.0.0

Then feed it into a cli called getobjectlst.exe with a -a flag that puts the subnet address from m-names.txt

This produces a report that looks like:

"167.227.31.14"  "brs2002_204"  "Server"        "Static"        "dc.dk.sark.com"
"167.227.31.15"  "brs2005_284"  "Server"        "Static"        "dc2.dk.sark.com"
"167.227.31.16"  "brs2003"      "Server"        "Static"        "dc4.dk.sark.com"

I am intrested in the 4th field entry value and how many (count) they are per $subnet=

I get a report on how many types etc... works great,

But I need it to also do another task, when I run this script I could run into a senerio like this:
Error 48: This subnet does not exist.
146.149.0.0
getobjectlst.exe :146.149.0.0:  Error code = 48

If I get an message like that, I need the script to engage in another routine;
If that subnet has that error 48" this subnet does not exist" then and run another cli called "getsubnetlst.exe" like so

 ./getsubnetlst.exe -u Xxx -p Xxx -a 146.149.0.0 -t netwrok -o rich

This will produce relatively the  same output as in the first code:
"East"  "146.149.1.0"  "N"  ""  "146.149.0.0"  " "  " "  "255.255.255.128"
"West-Subnet"  "146.149.16.0"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.252"
"North Subnet"  "146.149.16.128"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.240"
"Asia D-NAT"  "146.149.22.0"  "Y"  ""  "146.149.0.0"  " Japan"  " "  "255.255.255.0"

Then I need to run the same cli getobjectlst.exe and reference the subnet address in the second whitespace(146.149.1.0) for each one that It reports like on the above script on line 11

$ ./getobjectlst.exe -u Xxx-p Xxx -a 146.149.16.0 -o rich
"146.149.16.1"  "srv114096srv"  "Server"        "Static"        "chase.com"
"146.149.16.2"  "srv114097srv"  "Server"        "Static"        "chase.com"

For the report, I need it to be outputed as csv so I can import it into a spreadsheet, I envision it like so

 ( never mind on the header, I will create those in excel, but wanted to show.


Subnet                              Static                 DHCP                            Unused
146.149.16.0                      363                        0                                    57

I guess the output could be like this, then I will copy/paste it.
146.149.16.0,363,0,157

Then loop and do it all until its done.

Thanks for your help in advance !



#!perl
 
open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
    next unless /(.*?)\/(.*)$/;
    my $subnet = $1;
    print "Checking $subnet\n";
    my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
    
    my %counts;
    foreach my $line (@dnsoptions) {
        my @f = split/"\s+"/, $line;
        $counts{$f[3]}++;
    }
    
    print $log "For subnet $subnet:\n";
    foreach my $k (keys %counts) {
        print $log "    $k: $counts{$k}\n";
    }
}
 
close($log);

Open in new window

Avatar of richsark
richsark
Flag of United States of America image

ASKER

HI, I am just following up if I can get someone to help me please
Hi,

OK, I gave it a shot, see below, with my suprize, it has more errors then Microsoft :)



#!perl
 
open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names2.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
    next unless /(.*?)\/(.*)$/;
    my $subnet = $1;
    print "Checking $subnet\n";
    my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
    
    my %counts;
    foreach my $line (@dnsoptions) {
        my @f = split/"\s+"/, $line;
        $counts{$f[3]}++;
    }
            elsif ($line =~ /^\s*Error 48: This subnet does not exist.=([^\r\n]+)/) {
            my @subnetpart2 = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -o rich`;
            my @f = split/"\s+"/, $line;
            $counts{$f[3]}++;
         }
    
 printf $log "%s,%d,%d,%d\n", $subnet, ($counts{Static} or 0), ($counts{DHCP} or 0), ($counts{Unused} or 0);
 
close($log);

Open in new window

Hi, Could I kindly request some help?
Avatar of ozo
   foreach my $line (@dnsoptions) {
            if ($line =~ /^\s*Error 48: This subnet does not exist./) {
              my @subnetpart2 = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -o rich`;
          }
             my @f = split/"\s+"/, $line;
             $counts{$f[3]}++;
     }

#but I'm not sure what you want to do with @subnetpart2
HI Ozo,

subnetpart2 will execute getsubnetlst.exe which will produce a report like this:

East"  "146.149.1.0"  "N"  ""  "146.149.0.0"  " "  " "  "255.255.255.128"
"West-Subnet"  "146.149.16.0"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.252"
"North Subnet"  "146.149.16.128"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.240"
"Asia D-NAT"  "146.149.22.0"  "Y"  ""  "146.149.0.0"  " Japan"  " "  "255.255.255.0"

Then I need to run the same cli getobjectlst.exe and reference the subnet address in the second whitespace(146.149.1.0) for each one that It reports

$ ./getobjectlst.exe -u Xxx-p Xxx -a 146.149.16.0 -o rich
"146.149.16.1"  "srv114096srv"  "Server"        "Static"        "sark.com"
"146.149.16.2"  "srv114097srv"  "Server"        "Static"        "sark.com"

Then continue down the list until down, then start all over and go to the next subnet in the m-names.txt and so on and so forth until all done

West-Subnet"  "146.149.16.0"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.252"
"North Subnet"  "146.149.16.128"  "Y"  ""  "146.149.0.0"  " "  " "  "255.255.255.240"
"Asia D-NAT"  "146.149.22.0"  "Y"  ""  "146.149.0.0"  " Japan"  " "  "255.255.255.0"

Does that make sence?
HI, does that make sence? and will you above correction do it?

Let me know
Hi,

Any experts want to assist me on figuring out the missing link ?
HI, have a pieced this new code together from other folks who assisted me at:

http://www.unix.com/shell-programming-scripting/108484-count-script-wrapper-help-2.html

Anyway, I still cant get passed the if statement if Error 48 exists!

Any experts want to help me?


#!perl -w
my %counts;
my @subnettype;
my @dnsoptions;
my @dnsoptions2;
my @subnetpart2;
my $snetpart2;
my $subnet;
open(my $log, ">log-external-.txt") or die "Could not open log: $!\n";
printf $log "Subnet,Static,DHCP,Unused\n";
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
#next unless /(.*?)\/(.*)$/;
#next unless /(.*?)$/;
chomp;
$subnet = $_;
print "Checking $subnet\n";
@dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet -o Rich`;
# 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 (not defined($dnsoptions[0])) {
print "dnsoptions is null or undefined, going on to the next subnet\n";
next;
 
if (join(" ",@dnsoptions) =~ /Error 48: This subnet does not exist./) {
 
# call "getsubnetlst.exe", passing $subnet as one of the parameters
@subnetpart2 = `./getsubnetlst.exe -u xx-p xx -a $subnet -t network -o Rich`;
# 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
$snetpart2 = (split/"\s+"/, $line)[1];
# and now pass it to "getobjectlst.exe"; assign the output to
# the array @dnsoptions2
@dnsoptions2 = `./getobjectlst.exe -u xx -p xx -a $snetpart2 -o Rich`;
# find out counts of each subnettype (4th field from left)
foreach my $line (@dnsoptions2) {
@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) {
@subnettype = split/"\s+"/, $line;
$counts{$subnettype[3]}++;
}
}
printf $log "%s,%d,%d,%d\n", $subnet, ($counts{Static} or 0), ($counts{DHCP} or 0), ($counts{Unused} or 0);
%counts = ();
}
}
close($in);
close($log);

Open in new window

Avatar of Adam314
Adam314


open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
	next unless /(.*?)\/(.*)$/;
	my $subnet = $1;
	print "Checking $subnet\n";
	my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
    my %counts;
	if(join("",@dnsoptions) =~ /Error 48: This subnet does not exist/i) {
		my @newlines = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -o rich`;
		foreach my $line2 (@newlines) {
			my $subnet2 = (split(/"\s+"/, $line2))[1];
			@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet2 -o rich`;
			foreach my $line (@dnsoptions)) {
				my @f = split/"\s+"/, $line;
				$counts{$f[3]}++;
			}
	    	print $log "For subnet $subnet:\n";
			foreach my $k (keys %counts) {
				print $log "    $k: $counts{$k}\n";
			}
		}
	}
	else {
		foreach my $line (@dnsoptions)) {
			my @f = split/"\s+"/, $line;
			$counts{$f[3]}++;
		}
    	print $log "For subnet $subnet:\n";
		foreach my $k (keys %counts) {
			print $log "    $k: $counts{$k}\n";
		}
	}
}
 
close($log);

Open in new window

Thanks Adam, will try and get back to you.

One day I want to be as smart as you :)



Hi Adam, I could not resists to try it, but I got some errors:

$ perl adam.pl
Bareword found where operator expected at adam.pl line 2, near ")ordie"
        (Missing operator before ordie?)
Unquoted string "ordie" may clash with future reserved word at adam.pl line 2.
String found where operator expected at adam.pl line 2, near "ordie"Could not open log: $!\n""
Unquoted string "nextunless" may clash with future reserved word at adam.pl line 7.
Backslash found where operator expected at adam.pl line 7, near ")\"
        (Missing operator before \?)
"my" variable $subnet2 masks earlier declaration in same scope at adam.pl line 16.
"my" variable @dnsoptions masks earlier declaration in same scope at adam.pl line 17.
"my" variable $line masks earlier declaration in same statement at adam.pl line 18.
"my" variable @f masks earlier declaration in same scope at adam.pl line 19.
"my" variable $subnet masks earlier declaration in same scope at adam.pl line 21.
"my" variable %counts masks earlier declaration in same scope at adam.pl line 22.
"my" variable @f masks earlier declaration in same scope at adam.pl line 30.
syntax error at adam.pl line 2, near ")ordie"
syntax error at adam.pl line 7, near "(."
syntax error at adam.pl line 14, near "){"
syntax error at adam.pl line 17, near "))"
syntax error at adam.pl line 22, near "){"
syntax error at adam.pl line 25, near "}"
syntax error at adam.pl line 31, near "}"
syntax error at adam.pl line 33, near "){"
Execution of adam.pl aborted due to compilation errors.
Hi, Any thoughts?
Sounds like you are missing the space between "or" and "die" on the open statement.
HI,

Yup, I thought of that already, I had already changed that after I posted, here is the message  without the ordie

$ perl adam.pl
Unquoted string "nextunless" may clash with future reserved word at adam.pl line 13.
Backslash found where operator expected at adam.pl line 13, near ")\"
        (Missing operator before \?)
"my" variable $subnet2 masks earlier declaration in same scope at adam.pl line 22.
"my" variable @dnsoptions masks earlier declaration in same scope at adam.pl line 23.
"my" variable $line masks earlier declaration in same statement at adam.pl line 24.
"my" variable @f masks earlier declaration in same scope at adam.pl line 25.
"my" variable $subnet masks earlier declaration in same scope at adam.pl line 27.
"my" variable %counts masks earlier declaration in same scope at adam.pl line 28.
"my" variable %counts masks earlier declaration in same scope at adam.pl line 36.
"my" variable @f masks earlier declaration in same scope at adam.pl line 36.
syntax error at adam.pl line 13, near "(."
syntax error at adam.pl line 20, near "){"
syntax error at adam.pl line 23, near "))"
syntax error at adam.pl line 28, near "){"
syntax error at adam.pl line 31, near "}"
syntax error at adam.pl line 37, near "}"
syntax error at adam.pl line 39, near "){"
Execution of adam.pl aborted due to compilation errors.


Looks like a similar error between "next" and "unless" on line 13.
Hi,
what do you mean?
OK, I figured it out ;)

but now I got:

$ perl adam.pl
syntax error at adam.pl line 16, near "){"
syntax error at adam.pl line 19, near "))"
syntax error at adam.pl line 24, near "){"
syntax error at adam.pl line 27, near "}"
syntax error at adam.pl line 33, near "}"
syntax error at adam.pl line 35, near "){"
Execution of adam.pl aborted due to compilation errors.
Copy and paste it exactly as it is here.
#!/usr/bin/perl
open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
	next unless /(.*?)\/(.*)$/;
	my $subnet = $1;
	print "Checking $subnet\n";
	my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
	my %counts;
	if(join("",@dnsoptions) =~ /Error 48: This subnet does not exist/i) {
		my @newlines = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -o rich`;
		foreach my $line2 (@newlines) {
			my $subnet2 = (split(/"\s+"/, $line2))[1];
			@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet2 -o rich`;
			foreach my $line (@dnsoptions) {
				my @f = split/"\s+"/, $line;
				$counts{$f[3]}++;
			}
			print $log "For subnet $subnet:\n";
			foreach my $k (keys %counts) {
				print $log "    $k: $counts{$k}\n";
			}
		}
	}
	else {
		foreach my $line (@dnsoptions) {
			my @f = split/"\s+"/, $line;
			$counts{$f[3]}++;
		}
		print $log "For subnet $subnet:\n";
		foreach my $k (keys %counts) {
			print $log "    $k: $counts{$k}\n";
		}
	}
}
 
close($log);

Open in new window

I did as you said, still does not run the Error 48's

Also, is there a way we can suppress the error 48's with

running getobjectlst for Error48

$ perl adam4.pl

Checking 10.0.0.0
Checking 146.149.0.0
Error 48: This subnet does not exist.
146.149.0.0
qgetobjectlst.exe :146.149.0.0:  Error code = 48
Checking 161.16.0.0
Error 48: This subnet does not exist.
161.16.0.0

from the log.txt

For subnet 10.0.0.0:
    Static: 37
    Unused: 217
For subnet 146.149.0.0:
For subnet 161.16.0.0:
For subnet 161.56.0.0:
For subnet 162.2.0.0:
For subnet 162.5.0.0:

Also, once we get this worked out, I still would like the output to be like

146.149.16.0,363,0,157

HI,

I found a type on
my @newlines = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -o rich`;

Changed to:

my @newlines = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t network -o rich`;

Still no difference

#!/usr/bin/perl
open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
while(<$in>) {
        next unless /(.*?)\/(.*)$/;
        my $subnet = $1;
        print "Checking $subnet\n";
        my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
        my %counts;
        if(join("",@dnsoptions) =~ /Error 48: This subnet does not exist/i) {
                my @newlines = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t network -o rich`;
                foreach my $line2 (@newlines) {
                        my $subnet2 = (split(/"\s+"/, $line2))[1];
                        @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet2 -o rich`;
                        foreach my $line (@dnsoptions) {
                                my @f = split/"\s+"/, $line;
                                $counts{$f[3]}++;
                        }
                        print $log "For subnet $subnet:\n";
                        foreach my $k (keys %counts) {
                                print $log "    $k: $counts{$k}\n";
                        }
                }
        }
        else {
                foreach my $line (@dnsoptions) {
                        my @f = split/"\s+"/, $line;
                        $counts{$f[3]}++;
                }
                print $log "For subnet $subnet:\n";
                foreach my $k (keys %counts) {
                        print $log "    $k: $counts{$k}\n";
                }
        }
}
 
close($log);

Open in new window

if the getobjectlst program writes to stderr instead of stdout, you can fix it with:
    my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich 2>&1`;
OK, on line 16, should it be (looking at $subnet)

from:
@dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
                       
To this:
my @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet2 -o rich 2>&1`;

Thanks
HI Adam, not sure... Its still doing the same thing bro.
$ perl adam4.pl
Checking 10.0.0.0
Checking 146.149.0.0
Error 48: This subnet does not exist.
146.149.0.0
getobjectlst.exe :146.149.0.0:  Error code = 48
I used:
my @dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet2 -o rich 2>&1`;
and this way:
my @dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet -o rich 2>&1`;  
output from the log.txt
For subnet 10.0.0.0:
    Static: 37
    Unused: 217
For subnet 146.149.0.0:
For subnet 161.16.0.0:

This is a tough one !
I also used:
my @dnsoptions = `./getobjectlst.exe -u xx -p xx -a $subnet -o rich >&1`;  
 
HI Adam,
I ventured off and got some help from another site. I appericate your help on this !
This is the code that is working.  However I am getting an EOF error.
I opened another thread on this. I hope its the right thing to do?
Thanks
Below code taken from: ( Thanks to FishMonger)
http://perlguru.com/gforum.cgi?do=post_view_flat;post=37667;page=1;mh=-1;;sb=post_latest_reply;so=ASC 

#!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

ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Adam, ok will do, The thread is located at:

https://www.experts-exchange.com/questions/24380006/Help-with-EOF-error-in-script.html

Thanks see you there !


Thanks once more