Subversion Repositories sysadmin_scripts

Rev

Rev 78 | 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
 
71 rodolico 13
# a group is defined as 10+ pound signs, some text, then 10+ pound signs
78 rodolico 14
my $currentGroup = 'Unknown';
71 rodolico 15
my $record;
16
my $host = '';
78 rodolico 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
}
71 rodolico 28
 
78 rodolico 29
while ( $currentLine < @contents ) {
71 rodolico 30
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
31
      # special kind of comment used for grouping
32
      $currentGroup = $1;
33
      $currentGroup =~ s/^\s+|\s+$//g
34
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
78 rodolico 35
      $currentLine++;
71 rodolico 36
      next;
37
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
78 rodolico 38
      $currentLine++;
71 rodolico 39
      next;
40
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
41
      my $key = lc $1;
42
      my $value = lc $2;
43
      if ( $key eq 'host' ) {
44
         # if host already defined, this is a new record
45
         $host = $value;
46
      } else {
47
         $info->{$currentGroup}->{$host}->{$key} = $value;
48
      }
49
   }
78 rodolico 50
   $currentLine++;
70 rodolico 51
}
71 rodolico 52
#die Dumper( $info );
53
 
54
print Dump( $info );
55
 
56
#print STDERR scalar( keys @$info ) . " Entried Processed\n";
70 rodolico 57
1;