Subversion Repositories sysadmin_scripts

Rev

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

#! /usr/bin/env perl

use warnings;
use strict;

use YAML::Tiny;
use Data::Dumper;

my @contents = <>;
chomp @contents;
my $info;

sub skipHeader {
   my $currentLine = 0;
   my $line;
   while ( defined( $line = shift ) ) {
      last if $line =~ m/Host \*/;
      $currentLine++;
   }
   return 0 if $currentLine >= @_;
   while ( defined( $line = shift ) ) {
      last if $line !~ m/^[#\s]/;
      $currentLine++;
   }
   return $currentLine;
}


# a group is defined as 10+ pound signs, some text, then 10+ pound signs
my $currentGroup = '';
my $record;
my $host = '';

my $currentLine = &skipHeader( @contents ); # blows past stuff we're not interested in
while ( ++$currentLine < @contents ) {
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
      # special kind of comment used for grouping
      $currentGroup = $1;
      $currentGroup =~ s/^\s+|\s+$//g
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
      next;
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
      next;
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
      my $key = lc $1;
      my $value = lc $2;
      if ( $key eq 'host' ) {
         # if host already defined, this is a new record
         $host = $value;
      } else {
         $info->{$currentGroup}->{$host}->{$key} = $value;
      }
   }
}
#die Dumper( $info );

print Dump( $info );

#print STDERR scalar( keys @$info ) . " Entried Processed\n";
1;