App-DHCPClientUtils

 view release on metacpan or  search on metacpan

scripts/multi-homed-routing.pl  view on Meta::CPAN

use constant POLICY_WEIGHTED => 'weighted';
use constant POLICY_SINGLE   => 'single';

use constant RESERVED_TABLES    => {
                                    255 => 'local',
                                    254 => 'main',
                                    253 => 'default'
                                };


sub DisplayInterfaceInfo($)
{
    my ($interfaces) = @_;

    for my $if (@$interfaces) {
        my $info = $if->info();
        next if (!$info->{Net::Interface->af_inet()});
        print "interface = $if->name\n========================\n";
        print "IPv4 addrs= ", $info->{Net::Interface->af_inet()}->{number}, "\n";
        my @ipv4_addresses = $if->address(Net::Interface->af_inet());
        print "addr =      ", Net::Interface::inet_ntoa($ipv4_addresses[0]), "\n",

scripts/multi-homed-routing.pl  view on Meta::CPAN


            # Note: RFC2132 specifies, that there can exist multiple routers.
            # https://tools.ietf.org/html/rfc2132
            print "gateway =   ", $lease->option()->{routers}, "\n";
        }
        print "\n";
    }
}


sub SuggestSettings($)
{
    my ($interfaces) = @_;
    my @cmd = ();

    my $rt_table_idx = $rt_table_start_number;
    my $rt_table = _read_routing_tables();
    my $first_suggestion = 1;
    for my $if (@$interfaces) {
        my $info = $if->info();
        next if (!$info->{Net::Interface->af_inet()});

scripts/multi-homed-routing.pl  view on Meta::CPAN

        ++$rt_table_idx;
    }
    
    die "Detect failed!" if (!@cmd);

    print "\n" if (!$first_suggestion);
    printf("Run commad:\n%s %s\n", $0, join(' ', @cmd));
}


sub _read_routing_tables()
{
    # For table syntax, see: http://linux-ip.net/html/routing-tables.html
    my $tables = {};
    my $rt_table_re = qr/^\s*(\d+)\s+(\S+)/;
    open(RT_TABLE, $rt_table_path) or
        die "Failed to read IProuting2 table! Error: $!";
    while (<RT_TABLE>) {
        chomp();
        next if (!/$rt_table_re/);
        my $table_nro = $1;

scripts/multi-homed-routing.pl  view on Meta::CPAN


#
# @param    $interfaces
#           Array of interface-objects:
#           device =>   Interface device name, eg. eth0
#           table =>    Routing-table to use from /etc/iproute2/rt_tables
#           address =>  IPv4 address of the interface
#           network =>  Network of the interface in the form, IPv4-address/netmask
#           gateway =>  IPv4 address of the gateway
#           weight =>   Weight of the route
sub routing_rules($$$)
{
    my ($interfaces, $default_policy, $single_policy_default_interface) = @_;

    my $config = {
        INTERPOLATE => 1,               # expand "$var" in plain text
        EVAL_PERL   => 1,               # evaluate Perl code blocks
        PRE_CHOMP   => 0,
        POST_CHOMP  => 1 # CHOMP_GREEDY,
    };

scripts/multi-homed-routing.pl  view on Meta::CPAN


    # Go create a setup-file from the template!
    my $rules;
	$tt->process(\$template, $values, \$rules) or
        die sprintf("Template error: %s", $tt->error());

    return $rules;
}


sub _calculate_network($$)
{
    my ($ipv4_address, $netmask) = @_;

    my $net = new Net::IP($ipv4_address) or
        die "Failed to construct Net::IP. Error: " . Net::IP::Error();
    my $mask = new Net::IP($netmask);

    # Do IPv4 32-bit arithmetic:
    # Use the netmask to mask off "host" bits from the address.
    # Remaining bits are the "network" bits.

scripts/multi-homed-routing.pl  view on Meta::CPAN

    $mask =~ s/0+$//;       # Drop off zeros at the end
    $mask = length($mask);  # String length tells the number of bits in mask

    # Finalize the operation:
    my $network = "$net_ip_str/$mask";
    
    return $network;
}


sub main()
{
    my %opts;
    $opts{'accept-private-dhcp-addresses'} = 0;
    @orig_args = @ARGV;
    GetOptions(\%opts,
                "help|h",
                "version|V",
                "show|s",
                "detect|d",
                "interface|i=s@",

scripts/network-interface-info.pl  view on Meta::CPAN

use POSIX qw();
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;

use warnings;
use strict;



sub DisplayInterfaceInfo($)
{
    my ($interfaces) = @_;

    my @paths_to_attempt = ('/var/lib/dhclient', '/var/lib/dhcp', '/var/lib/NetworkManager');

    for my $if (@$interfaces) {
        my $info = $if->info();
        my $flags = $if->flags();
        next if (!$info->{Net::Interface->af_inet()});

scripts/network-interface-info.pl  view on Meta::CPAN

            } else {
                print("lease is expired\n");
            }
        }

        # IPv4 and IPv6 done.
        print "\n";
    }
}

sub main()
{
    my %opts;
    GetOptions (\%opts,
                "help|h",
                "version|v",
                "show|s",
                "interface|i=s@"
    ) or die "Malformed arguments! Stopped.";

    pod2usage(-exitval => 0, -verbose => 1) if (defined($opts{help}));



( run in 0.602 second using v1.01-cache-2.11-cpan-65fba6d93b7 )