> -----Original Message-----
> From: Blackburn, David W [mailto:[EMAIL PROTECTED]]
> Subject: Sudoers Regex
> 
> Hi

Hello.
> 
> I am trying to parse a string of text from a sudoers file, 
> what I am trying
> to get out is the HOSTCLUSTER name and the hosts.
> 
> Host_Alias      HOSTCLUSTER =   host1, host2, \
>                                 host3, host4, \
>                                 host5, host6 
> 
> The first problem is I split using "=" so $hostAlias[0] contains
> "Host_Alias      HOSTCLUSTER" and I just need to return the 
> HOSTCLUSTER part
> and disgard Host_Alias so I will need a regex for that.
> 
> And the second bit needs to return the hosts
> 
> I know there are no examples but I have tried numerous ways 
> and cannot solve
> this..
> 
 <snip...>

I assume you need all 6 hosts for each hostcluster.  Following is a program
that does that.  If you have questions about exactly what is happening,
please send me a message and I'll be happy to go through it line by line for
you...

Hope this helps...
Jason

use warnings;
use strict;

my %clusters = ();
my $sudoer_file = "sudoers.current";
open (FH, "<$sudoer_file") || die "Couldn't open $sudoer_file: $!";
while (defined (my $line = <FH>)) {
        next unless ($line =~ /^Host_Alias/);
        my ($discard, $cluster_name, @hosts) = split(/\W+/, $line);
        while ($line =~ m#\\$#) {
                my $new_line = <FH>;
                $new_line = Trim($new_line);
                push (@hosts, (split (/\W+/, $new_line)));
                $line = $new_line;
        }
        $clusters{$cluster_name}{Hosts} = \@hosts;
}
foreach my $cluster (sort keys %clusters) {
        print "\n$cluster contains:\n";
        my $hosts = $clusters{$cluster}{Hosts};
        foreach my $host (@$hosts) {
                print "  $host\n";
        }
}
close (FH) || die "Can't close $sudoer_file: $!";

#this subroutine trims leading and trailing white space from a variable
#it accepts either a variable or an array as its only argument
sub Trim {
        my @out = @_;
        for (@out) {
                s/^\s+//;
                s/\s+$//;
        }
        return wantarray ? @out : $out[0];
}


CONFIDENTIALITY NOTICE:

************************************************************************

The information contained in this ELECTRONIC MAIL transmission
is confidential.  It may also be privileged work product or proprietary
information. This information is intended for the exclusive use of the
addressee(s).  If you are not the intended recipient, you are hereby
notified that any use, disclosure, dissemination, distribution [other
than to the addressee(s)], copying or taking of any action because
of this information is strictly prohibited.

************************************************************************

Reply via email to