Subversion Repositories sysadmin_scripts

Rev

Rev 24 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24 rodolico 1
#! /usr/bin/perl -w
2
 
3
my $sshFile = '/etc/ssh/ssh_config';
4
open SSH,"<$sshFile" or die "could not read $sshFile: $!\n";
5
my @contents = <SSH>;
6
close SSH;
7
 
8
chomp @contents;
9
 
10
sub getNextBlock {
11
   my $currentLine = shift;
12
   my $name = '';
13
   my $target = '';
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;
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
      }
33
      $currentLine++;
34
    }
35
    return ( $currentLine, '' );
36
}
37
 
38
sub skipHeader {
39
   my $toFind = '#      System Wide Aliases for all users';
40
   $currentLine = 0;
41
   while ( $currentLine < @contents && $contents[$currentLine] !~ m/$toFind/ ) {
42
      $currentLine++;
43
   }
44
   return $currentLine;
45
}
46
 
47
 
48
my $line = &skipHeader();
49
my @info;
50
while ( $line < @contents ) {
51
   my $targets;
52
   ( $line, $targets ) = &getNextBlock( $line );
53
#   print "$line\t$targets\n";
54
   push @info, $targets;    
55
}
56
print "NumEntries = " . scalar( @info ) . "\n";
57
print "Host\tTarget\tUser\tPort\n" . join( "\n", @info ) . "\n";
58
1;