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