#!/usr/bin/perl -w # by tzz@iglou.com use Data::Dumper; # for quick testing use POSIX; # you'll need it eventually use strict; use AppConfig qw/:expand :argcount/; $| = 1; # auto-flush the output # define the configuration variables - see the AppConfig documentation # for details my $config = AppConfig->new(); $config->define( 'HELP' => { ARGCOUNT => ARGCOUNT_NONE,DEFAULT => 0}, # default machine 'MACHINE' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 0}, # optional port 'PORT' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 0}, # default service 'SERVICE' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => 0}, # default directory for service file 'XINETDDIR' => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => '/etc/xinetd.d/'}, ); # now read the command-line options from @ARGV die "Invalid options passed, quitting" unless $config->args(); # set 'HELP' if necessary $config->HELP(1) unless ($config->MACHINE && $config->SERVICE); # handle help requests, or warn if -machine and -service were not specified if ($config->HELP) { print <XINETDDIR . $config->SERVICE; open SVC, $service_filename or die "Couldn't open $service_filename for reading: $!"; my @service_lines = ; close SVC; if (grep /^\s*redirect\s*=.*/, @service_lines) { my $replace = $config->MACHINE; # build the replacement redirection $replace .= ' ' . $config->PORT if $config->PORT; # apply the replacement redirection to the service file map { s/^(\s*redirect\s*=\s*).*/$1$replace\n/ } @service_lines; print "Modified service file:\n"; print "-----------------------\n"; print @service_lines; print "-----------------------\n"; } open SVC, ">$service_filename" or die "Couldn't open $service_filename for writing: $!"; print SVC @service_lines; close SVC;