Subversion Repositories sysadmin_scripts

Rev

Rev 71 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 71 Rev 78
Line 8... Line 8...
8
 
8
 
9
my @contents = <>;
9
my @contents = <>;
10
chomp @contents;
10
chomp @contents;
11
my $info;
11
my $info;
12
 
12
 
13
sub skipHeader {
-
 
14
   my $currentLine = 0;
-
 
15
   my $line;
-
 
16
   while ( defined( $line = shift ) ) {
-
 
17
      last if $line =~ m/Host \*/;
-
 
18
      $currentLine++;
-
 
19
   }
-
 
20
   return 0 if $currentLine >= @_;
-
 
21
   while ( defined( $line = shift ) ) {
-
 
22
      last if $line !~ m/^[#\s]/;
-
 
23
      $currentLine++;
-
 
24
   }
-
 
25
   return $currentLine;
-
 
26
}
-
 
27
 
-
 
28
 
-
 
29
# a group is defined as 10+ pound signs, some text, then 10+ pound signs
13
# a group is defined as 10+ pound signs, some text, then 10+ pound signs
30
my $currentGroup = '';
14
my $currentGroup = 'Unknown';
31
my $record;
15
my $record;
32
my $host = '';
16
my $host = '';
-
 
17
my $currentLine = 0;
-
 
18
# skip anything up until we find the first entry
-
 
19
# first entry is defined as
-
 
20
# constant 'host' in first column
-
 
21
# 1 or more spaces
-
 
22
# a token (word) of alphanumeric, period and dash
-
 
23
# no trailing blank space
-
 
24
while ( $currentLine < @contents ) {
-
 
25
   last if $contents[$currentLine] =~ m/^host\s+[a-z0-9.-]+$/i;
-
 
26
   $currentLine++;
-
 
27
}
33
 
28
 
34
my $currentLine = &skipHeader( @contents ); # blows past stuff we're not interested in
-
 
35
while ( ++$currentLine < @contents ) {
29
while ( $currentLine < @contents ) {
36
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
30
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
37
      # special kind of comment used for grouping
31
      # special kind of comment used for grouping
38
      $currentGroup = $1;
32
      $currentGroup = $1;
39
      $currentGroup =~ s/^\s+|\s+$//g
33
      $currentGroup =~ s/^\s+|\s+$//g
40
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
34
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
-
 
35
      $currentLine++;
41
      next;
36
      next;
42
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
37
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
-
 
38
      $currentLine++;
43
      next;
39
      next;
44
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
40
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
45
      my $key = lc $1;
41
      my $key = lc $1;
46
      my $value = lc $2;
42
      my $value = lc $2;
47
      if ( $key eq 'host' ) {
43
      if ( $key eq 'host' ) {
Line 49... Line 45...
49
         $host = $value;
45
         $host = $value;
50
      } else {
46
      } else {
51
         $info->{$currentGroup}->{$host}->{$key} = $value;
47
         $info->{$currentGroup}->{$host}->{$key} = $value;
52
      }
48
      }
53
   }
49
   }
-
 
50
   $currentLine++;
54
}
51
}
55
#die Dumper( $info );
52
#die Dumper( $info );
56
 
53
 
57
print Dump( $info );
54
print Dump( $info );
58
 
55