Subversion Repositories sysadmin_scripts

Rev

Rev 70 | Rev 78 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
71 rodolico 1
#! /usr/bin/env perl
70 rodolico 2
 
71 rodolico 3
use warnings;
4
use strict;
70 rodolico 5
 
71 rodolico 6
use YAML::Tiny;
7
use Data::Dumper;
8
 
9
my @contents = <>;
70 rodolico 10
chomp @contents;
71 rodolico 11
my $info;
70 rodolico 12
 
13
sub skipHeader {
71 rodolico 14
   my $currentLine = 0;
15
   my $line;
16
   while ( defined( $line = shift ) ) {
17
      last if $line =~ m/Host \*/;
70 rodolico 18
      $currentLine++;
19
   }
71 rodolico 20
   return 0 if $currentLine >= @_;
21
   while ( defined( $line = shift ) ) {
22
      last if $line !~ m/^[#\s]/;
23
      $currentLine++;
24
   }
70 rodolico 25
   return $currentLine;
26
}
27
 
28
 
71 rodolico 29
# a group is defined as 10+ pound signs, some text, then 10+ pound signs
30
my $currentGroup = '';
31
my $record;
32
my $host = '';
33
 
34
my $currentLine = &skipHeader( @contents ); # blows past stuff we're not interested in
35
while ( ++$currentLine < @contents ) {
36
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
37
      # special kind of comment used for grouping
38
      $currentGroup = $1;
39
      $currentGroup =~ s/^\s+|\s+$//g
40
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
41
      next;
42
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
43
      next;
44
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
45
      my $key = lc $1;
46
      my $value = lc $2;
47
      if ( $key eq 'host' ) {
48
         # if host already defined, this is a new record
49
         $host = $value;
50
      } else {
51
         $info->{$currentGroup}->{$host}->{$key} = $value;
52
      }
53
   }
70 rodolico 54
}
71 rodolico 55
#die Dumper( $info );
56
 
57
print Dump( $info );
58
 
59
#print STDERR scalar( keys @$info ) . " Entried Processed\n";
70 rodolico 60
1;