Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/extract-vhost

#!/usr/bin/perl
# encoding: utf-8
#
# author: Kyle Yetter
#

=pod

=head1 extract-vhost

extract-vhost - extract VirtualHost sections of the server's main httpd.conf matching the given text

=head1 SYNOPSIS

extract-vhost [ options ] text-to-match

=head1 DESCRIPTION

Reads in an apache configruation file (or the server's main httpd.conf if none is piped in) and
prints VirtualHost blocks containing the given term, such as a domain name or user name.

=head1 OPTIONS

=over 8

=item B<--man>

Show full script documentation as a man page

=item B<-h>, B<--help>

You know the drill

=item B<-v>, B<--version>

Ditto

=back

=head1 EXAMPLES

$] extract-vhost mysite.com
  <VirtualHost 1.2.3.4:80>
      ServerName mysite.com
      ServerAlias www.mysite.com
      DocumentRoot /home/mysite5/public_html
      ServerAdmin webmaster@mysite.com
      UseCanonicalName Off
      CustomLog /usr/local/apache/domlogs/mysite.com combined
      CustomLog /usr/local/apache/domlogs/mysite.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
      ## User mysite5 # Needed for Cpanel::ApacheConf
      <IfModule mod_suphp.c>
          suPHP_UserGroup mysite5 mysite5
      </IfModule>
      <IfModule !mod_disable_suexec.c>
          SuexecUserGroup mysite5 mysite5
      </IfModule>
      ScriptAlias /cgi-bin/ /home/mysite5/public_html/cgi-bin/

      Include "/usr/local/apache/conf/userdata/*.conf"
      Include "/usr/local/apache/conf/userdata/*.owner-root"
      Include "/usr/local/apache/conf/userdata/std/*.conf"
      Include "/usr/local/apache/conf/userdata/std/*.owner-root"
      Include "/usr/local/apache/conf/userdata/std/2/*.conf"
      Include "/usr/local/apache/conf/userdata/std/2/*.owner-root"

  </VirtualHost>

=cut

use Getopt::Long;
use Pod::Usage;


our $VERSION = 1.1;

Getopt::Long::Configure( 'bundling' );

GetOptions(
  'm|man'  => sub { pod2usage( { -exitval => 0, -verbose => 2 } ); },
  'h|help' => sub { pod2usage( 0 ); },
  'v|version' => sub { print "$VERSION\n"; exit( 0 ); }
);


our $pattern = shift or pod2usage( { -exitval => 1, -verbose => 2 } );
$pattern = quotemeta( $pattern );

if ( -t ) {
  if ( -e '/etc/cpanel/ea4/is_ea4' ) {
      open( STDIN, '/etc/apache2/conf/httpd.conf' );
  } else {
      open( STDIN, '/usr/local/apache/conf/httpd.conf' );
  }
}

our @vhost = ();
our $depth = 0;

while ( <> ) {
  if ( $depth > 0 ) {

    push( @vhost, $_ );

    if ( /<\s*VirtualHost/ ) {
      $depth += 1;
    } elsif ( m(<\s*/\s*VirtualHost) ) {
      $depth -= 1;
      if ( $depth == 0 ) {
        print( @vhost ) if grep /$pattern/i, @vhost;
        @vhost = ();
      }
    }

  } elsif ( /<\s*VirtualHost/ ) {
    push( @vhost, $_ );
    $depth += 1;
  }
}

Zerion Mini Shell 1.0