Subversion Repositories sysadmin_scripts

Rev

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

Rev 70 Rev 71
Line 1... Line 1...
1
#! /usr/bin/perl -w
1
#! /usr/bin/env perl
2
 
2
 
3
my $sshFile = '/etc/ssh/ssh_config';
-
 
4
open SSH,"<$sshFile" or die "could not read $sshFile: $!\n";
-
 
5
my @contents = <SSH>;
3
use warnings;
6
close SSH;
4
use strict;
7
 
5
 
8
chomp @contents;
6
use YAML::Tiny;
-
 
7
use Data::Dumper;
9
 
8
 
10
sub getNextBlock {
-
 
11
   my $currentLine = shift;
-
 
12
   my $name = '';
-
 
13
   my $target = '';
9
my @contents = <>;
14
   my $user = '';
-
 
15
   my $port = '';
-
 
16
   while ( $currentLine < @contents ) {
-
 
17
#      print "$currentLine\t$contents[$currentLine]\n";
-
 
18
      if ( $contents[$currentLine] =~ m/^\s*$/ ) { # blank line, skip it
-
 
19
         $currentLine++;
-
 
20
         next;
10
chomp @contents;
21
      }
-
 
22
      my ($key,$value) = split( /\s+/, $contents[$currentLine] );
-
 
23
      if ( lc( $key ) eq 'host' ) {
-
 
24
         return ($currentLine, join( "\t", ($name,$target,$user,$port) ) ) if $name;
-
 
25
         $name = $value;
-
 
26
      } elsif ( lc( $key ) eq 'hostname' ) {
-
 
27
         $target = $value;
-
 
28
      } elsif ( lc( $key ) eq 'user' ) {
-
 
29
         $user = $value;
-
 
30
      } elsif ( lc( $key ) eq 'port' ) {    
-
 
31
         $port = $value;
-
 
32
      }
11
my $info;
33
      $currentLine++;
-
 
34
    }
-
 
35
    return ( $currentLine, '' );
-
 
36
}
-
 
37
 
12
 
38
sub skipHeader {
13
sub skipHeader {
-
 
14
   my $currentLine = 0;
-
 
15
   my $line;
-
 
16
   while ( defined( $line = shift ) ) {
39
   my $toFind = '#      System Wide Aliases for all users';
17
      last if $line =~ m/Host \*/;
40
   $currentLine = 0;
18
      $currentLine++;
-
 
19
   }
-
 
20
   return 0 if $currentLine >= @_;
41
   while ( $currentLine < @contents && $contents[$currentLine] !~ m/$toFind/ ) {
21
   while ( defined( $line = shift ) ) {
-
 
22
      last if $line !~ m/^[#\s]/;
42
      $currentLine++;
23
      $currentLine++;
43
   }
24
   }
44
   return $currentLine;
25
   return $currentLine;
45
}
26
}
46
 
27
 
47
 
28
 
-
 
29
# a group is defined as 10+ pound signs, some text, then 10+ pound signs
48
my $line = &skipHeader();
30
my $currentGroup = '';
49
my @info;
31
my $record;
-
 
32
my $host = '';
-
 
33
 
-
 
34
my $currentLine = &skipHeader( @contents ); # blows past stuff we're not interested in
50
while ( $line < @contents ) {
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
51
   my $targets;
41
      next;
52
   ( $line, $targets ) = &getNextBlock( $line );
42
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
-
 
43
      next;
-
 
44
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
53
#   print "$line\t$targets\n";
45
      my $key = lc $1;
-
 
46
      my $value = lc $2;
54
   push @info, $targets;    
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
   }
55
}
54
}
-
 
55
#die Dumper( $info );
-
 
56
 
56
print "NumEntries = " . scalar( @info ) . "\n";
57
print Dump( $info );
-
 
58
 
57
print "Host\tTarget\tUser\tPort\n" . join( "\n", @info ) . "\n";
59
#print STDERR scalar( keys @$info ) . " Entried Processed\n";
58
1;
60
1;