Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Count script wrapper help

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-29-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
Count script wrapper help

I have this a code that I got help with for another task. I since tried to modify it to work on this task.

I need someones expertise to modify it slightly and I am not sure where to start or yet fully understand the logic.

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 !

This is what I have thus far, but it does not work right. I am not a perl expert so any help would be awesome !

Thanks


Code:
#!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);

Sponsored Links
    #2  
Old Unix and Linux 04-29-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
Any takers want to help a brotha' out?
Sponsored Links
    #3  
Old Unix and Linux 04-30-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Maybe something like this. Please go through the script comments carefully to understand the flow of logic.


Code:
#!perl -w
my %counts;
my @subnettype;
my @dnsoptions;
my @dnsoptions2;
my @subnetpart2;
my $snetpart2;
my $subnet;
open(my $log, ">log-subnet.txt") or die "Could not open log: $!\n";
printf $log "Subnet,Static,DHCP,Unused\n";
##### Step 1, read subnets
open(my $in, "<m-names2.txt") or die "Could not open m-names2.txt: $!\n";
while(<$in>) {
  next unless /(.*?)\/(.*)$/;
  $subnet = $1;
  print "Checking $subnet\n";
  @dnsoptions = `./getobjectlst.exe -u Xxx-p Xxx -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 ($dnsoptions[0] =~ /Error 48: This subnet does not exist./) {
    # call "getsubnetlst.exe", passing $subnet as one of the parameters
    @subnetpart2 = `./getsubnetlst.exe -u Xxx -p Xxx -a $subnet -t netwrok -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 Xxx-p Xxx -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);

Hope that helps,
tyler_durden

_______________________________________________________________________________________
"If you don't claim your humanity you will become a statistic. You have been warned."
    #4  
Old Unix and Linux 04-30-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
HI,
Thanks for the explanation !

Will try it and let you know

Thanks a bunch !
Sponsored Links
    #5  
Old Unix and Linux 04-30-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
HI,

I ran your awesome code, but I got an error:

Error 48: This subnet does not exist.
10.2.0.0
getobjectlst.exe :10.2.0.0: Error code = 48
Use of uninitialized value $dnsoptions[0] in pattern match (m//) at update-enhan.pl line 23, <$in> line 3
Sponsored Links
    #6  
Old Unix and Linux 04-30-2009
richsark richsark is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 10 February 2016, 9:34 PM EST
Posts: 135
Thanks: 7
Thanked 0 Times in 0 Posts
Hi !

I made some mods on your code, have a look, but I still get that error that the subnet does not exists.


Code:
#!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-names2.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.
foreach my $line (@dnsoptions) {
            if ($line =~ /^\s*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);

Do you think we need more info on this line, to match the line, looks like its calling the subnet where the error is:

foreach my $line (@dnsoptions) {
if ($line =~ /^\s*Error 48: This subnet does not exist./) {

Error 48: This subnet does not exist.
146.149.0.0/16
Sponsored Links
    #7  
Old Unix and Linux 04-30-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by richsark View Post
...
Error 48: This subnet does not exist.
10.2.0.0
getobjectlst.exe :10.2.0.0: Error code = 48
Use of uninitialized value $dnsoptions[0] in pattern match (m//) at update-enhan.pl line 23, <$in> line 3
A couple of points here:

(1) I think the error is being thrown because the list @dnsoptions is undefined, i.e. the executable "getobjeclst.exe" probably returns a zero-length string in some cases.

(2) Again, it's quite possible that the output of "getobjectlst.exe" is something like this:


HTML Code:

Error 48: This subnet does not exist.
146.149.0.0
getobjectlst.exe :146.149.0.0: Error code = 48
i.e. the first line could be blank. In such a case, $dnsoptions[0] would be equal to "\n". Hence a comparison like this:


Code:
  if ($dnsoptions[0] =~ /Error 48: This subnet does not exist./) {

would fail, because the string "Error 48: This subnet does not exist." is in $dnsoptions[1], or maybe in $dnsoptions[2] etc.

(3) The logic you explained is as follows:

(a) take a subnet from "m-names2.txt", and pass it to "getobjectlst.exe"
(b) if an error is thrown, pass that subnet to "getsubnetlst.exe".
(c) loop through the output of "getsubnetlst.exe", pick the 2nd token, and call "getobjectlst.exe" with that as parameter.

Now, what if "getsubnetlst.exe" itself fails, or returns a null string ? The current script does not take care of that scenario.

======================================================

For problem # (1) above, I assume you just want to iterate to the next subnet value, maybe after printing an error message.

For problem # (2) above, you'll need to see if the string "Error 48: This subnet does not exist." occurs in any element of the list @dnsoptions (and not just in $dnsoptions[0]). You could join all elements of the list into a single string, and check for the occurrence of that error message in that string.

So the modification for (1) and (2) is as follows:


Code:
  ...
  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./) {
  ...

HTH,
tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
wrapper script in perl richsark Shell Programming and Scripting 5 03-23-2009 04:41 PM
Help with a wrapper script not working richsark Shell Programming and Scripting 0 03-09-2009 01:49 PM
Korn Shell Wrapper script pareshan Shell Programming and Scripting 6 12-23-2008 12:56 PM
What is a wrapper script thana UNIX for Dummies Questions & Answers 1 01-28-2008 07:11 AM
What is wrapper script and how to write chiru UNIX for Dummies Questions & Answers 1 06-12-2006 05:23 AM



All times are GMT -4. The time now is 04:00 PM.