1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#! /usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Hostfile::Manager;
use Term::Clui;
my @enabled = ();
my @disabled = ();
my $interactive = 0;
my $status = 0;
my $help = 0;
my $man = 0;
GetOptions(
"enable=s" => \@enabled,
"disable=s" => \@disabled,
"interactive" => \$interactive,
"status" => \$status,
"help|?" => \$help,
man => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
@enabled = split( /,/, join( ',', @enabled ) );
@disabled = split( /,/, join( ',', @disabled ) );
pod2usage(1) unless ( @enabled || @disabled || $status || $interactive );
my $manager = Hostfile::Manager->new;
if ($interactive) {
my $modified = 0;
while (
my $chosen = choose(
"Select a hostfile fragment:",
map { get_fragment_status_line($_) } $manager->fragment_list
)
)
{
$chosen =~ s/^..//;
$manager->toggle_fragment($chosen);
$modified = 1;
}
$manager->write_hostfile if $modified;
}
else {
map { print "Disabling $_\n"; $manager->disable_fragment($_) } @disabled;
map { print "Enabling $_\n"; $manager->enable_fragment($_) } @enabled;
$manager->write_hostfile if ( @disabled || @enabled );
map { print get_fragment_status_line($_) . "\n" } $manager->fragment_list
if $status;
}
sub get_fragment_status_line {
my $fragment_name = shift;
my $flag = $manager->fragment_status_flag($fragment_name);
return "$flag $fragment_name";
}
__END__
=head1 NAME
hostfiles - A simple script to manage multiple sets of hostfiles on *NIX systems
=head1 SYNOPSIS
hostfiles [options]
Options:
--enable Enable one or more hostfile fragments
--disable Disable one or more hostfile fragments
--status Display the status of various hostfile fragments
--interactive Present an interactive list of hostfile fragments
=head1 OPTIONS
=over 8
=item B<--enable>
Enable one or more hostfile fragments. This option may be specified multiple times and multiple fragments may be specified in a comma-separated list.
=item B<--disable>
Disable one or more hostfile fragments. This option may be specified multiple times and multiple fragments may be specified in a comma-separated list.
=item B<--status>
Display a list of fragments and their status. A '+' indicates the fragment is enabled. A '*' indicates an enabled fragment has been changed in /etc/hosts.
=item B<--interactive>
Presents an interactive list of hostfile fragment to enable or disable.
=back
=head1 DESCRIPTION
B<This program> will read the hostfile fragments specified to be enabled from /etc/hostfiles/<fragment_name> and add them to /etc/hosts. It will remove any fragments from /etc/hosts that are specified to be disabled.
=head1 LICENSE
Copyright (c) 2010-11 Anthony J. Mirabella. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Anthony J. Mirabella <mirabeaj AT gmail DOT com>
|