#!/usr/bin/perl use strict; use warnings; use CMS::MediaWiki; my $input_file = '/usr/local/perlib/sysadmin/servers.txt'; my %HOST_HASH; my %HOST_SHORTNAME_HASH; my $page_output; open( FILE, "<", $input_file ) || die "no file"; while () { my $line = $_; next unless ( $line =~ /Status/ ); my @values = split( /\s/, $line ); my $ip = $values[1]; my ($host) = $values[2] =~ /\((.*?)\)/; my ($hostname) = lc($host) =~ /^(.*?)\.my\.com$/; #-- In case the name does not parse if ( !$hostname ) { $hostname = lc($host); } $HOST_HASH{$ip} = $host; $HOST_SHORTNAME_HASH{$ip} = $hostname; } close(FILE); #--- Wiki ip to host list $page_output .= qq'{| border="2" cellpadding="5" cellspacing="0" ! ip || hostname' . "\n"; foreach my $ip ( sort { ( sprintf( "%03d", ( split( /\./, $a ) )[2] ) . sprintf( "%03d", ( split( /\./, $a ) )[3] ) ) <=> ( sprintf( "%03d", ( split( /\./, $b ) )[2] ) . sprintf( "%03d", ( split( /\./, $b ) )[3] ) ) } keys %HOST_HASH ) { $page_output .= '|- ' . "\n"; $page_output .= '| ' . $ip . ' || ' . $HOST_HASH{$ip} . "\n"; } $page_output .= '|}' . "\n"; #--- end ip to host list $page_output .= "\n"; $page_output .= "\n"; $page_output .= "\n"; #--- Wiki host to ip list $page_output .= qq'{| border="2" cellpadding="5" cellspacing="0" ! hostname || ip' . "\n"; foreach my $ip ( sort { lc( $HOST_HASH{$a} ) cmp lc( $HOST_HASH{$b} ) } keys %HOST_HASH ) { $page_output .= '|- ' . "\n"; $page_output .= '| ' . $HOST_HASH{$ip} . ' || ' . $ip . "\n"; } $page_output .= '|}' . "\n"; #--- end host to ip list $page_output .= "\n"; $page_output .= "\n"; $page_output .= "\n"; #--- Wiki ip to short host list for /etc/host $page_output .= qq'{| border="2" cellpadding="5" cellspacing="0" ! ip || hostname' . "\n"; foreach my $ip ( sort { ( sprintf( "%03d", ( split( /\./, $a ) )[2] ) . sprintf( "%03d", ( split( /\./, $a ) )[3] ) ) <=> ( sprintf( "%03d", ( split( /\./, $b ) )[2] ) . sprintf( "%03d", ( split( /\./, $b ) )[3] ) ) } keys %HOST_HASH ) { $page_output .= '|- ' . "\n"; $page_output .= '| ' . $ip . ' || ' . $HOST_SHORTNAME_HASH{$ip} . "\n"; } $page_output .= '|}' . "\n"; #--- end ip to host list $page_output .= "\n"; $page_output .= "\n"; $page_output .= "\n"; $page_output .= 'Page Updated via Perl nms01:/usr/local/perlib/sysadmin/wiki_facility_server_list.pl'; #print $page_output; my $mw = CMS::MediaWiki->new( # protocol => 'https', # Optional, default is http host => 'wiki.host.com', # Default: localhost path => '', # Can be empty on 3rd-level domain Wikis debug => 0 # Optional. 0=no debug msgs, 1=some msgs, 2=more msgs ); my $rc = $mw->editPage( title => 'Server_List', section => '', text => $page_output, summary => '', ); exit;