?
Solved

count script wrapper

Posted on 2009-04-23
11
Medium Priority
?
234 Views
Last Modified: 2012-05-06
Hello,

I feel shy... but this is as far as I could get

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=

Go ahead and laugh... below is my interpertation

#!perl
open(my $out, ">log-subnet.txt") or die "Could not open output: $!\n";
 
##### Step 1, read subnets
open(my $in, "<m-names.txt") or die "Could not open m-names.txt: $!\n";
next unless /(.*?),(.*)$/;
         
  next unless /(.*?),(.*)$/;
   my $subnet = $1;
   print "Checking $subnet\n";
   my $dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -a $subnet -o rich`;
   
   }
   print $out "$Subnet\n";
   print $out "There are ,"\$field4\" ";

Open in new window

0
Comment
Question by:richsark
  • 7
  • 4
11 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24216799
>>I am intrested in the 4th field entry value and how many (count) they are per $subnet=
I'm not sure what you mean by this.

But, for the first part, a few problems with your script:
1) you aren't reading from the m-names.txt file - you open it, but then never read from it
2) your RE is splitting on comma, not /.

Try this version:
#!perl
 
##### 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`;
    
    #Not sure what you want to do with that output...
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 24217019
Hi Adam,

My output from
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"

The fourth fied which is "Static" I guess you call it a field ??

I need a report showing separated by every subnet I have X amount ( whatever is in this field) it could read Static, DHCP, M-DHCP etc... But I hope the code will read whatever is there so we dont have to specify it.

Does this help answer it?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24217522
As long as the format is always the same.... meaning words seperated by double-quote, whitespace, double-quote, this should work:

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

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: 24218530
Yup, all the same, I will try it
Thanks
0
 
LVL 1

Author Comment

by:richsark
ID: 24224087
Hi Adam,
I received this message
Search pattern not terminated at count-scr.pl line 15
0
 
LVL 1

Author Comment

by:richsark
ID: 24226247
Hi, I was able to get some help, and found that
This
my @f = split/"\s+", $line;
should be
my @f = split/"\s+"/, $line;
I will test and be back
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24228125
Yes, I was missing the closing /.  Let me know how it goes.
0
 
LVL 1

Author Comment

by:richsark
ID: 24249912
Hi,
Slight change needed on the output part in terms of how its formated.
The orginal format was great, but hard to put and sort in excel. Is there a way to make the output in a comma seperated?
For example: ( never mind on the header, I will create those in excel, but wanted to show.

Subnet                              Static                 DHCP                            Unused
169.2.1.0                           363                        0                                    157

 I guess the output could be like this, then I will copy/paste it.
169.2.1.0,363,0,157
Thanks
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24251789
Replace lines 19-22 with this

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

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 24259696
OK, testing now..
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 31573832
Good work as always
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 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