source: trunk/LATMOS-Accounts/bin/la-freeip @ 984

Last change on this file since 984 was 861, checked in by nanardon, 13 years ago
  • reimport missing files from previous svn
  • Property svn:executable set to *
File size: 1.4 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts::BuildNet;
6use Getopt::Long;
7use Pod::Usage;
8
9=head1 NAME
10
11    la-freeip - Search unused ip in database
12
13=head1 SYNOPSIS
14
15    la-buildnet [-f|-r] zone [...]
16
17=cut
18
19GetOptions(
20    'c|config=s'        => \my $config,
21    'b|base=s'          => \my $base,
22    'help'              => sub { pod2usage(0) },
23    'n'                 => \my $network,
24    'f|first'           => \my $first,
25    'r|random'          => \my $random,
26) or pod2usage();
27
28=head1 OPTIONS
29
30=over 4
31
32=item -c|--config configdir
33
34Use this configuration directory instead of the default one.
35
36=item -b|--base basename
37
38Query this specific base instead of the default one.
39
40=item -f|--first
41
42Return the first free ip
43
44=item -r|--random
45
46Return a random free ip in the unallocated pool
47
48=back
49
50=cut
51
52my $LA = LATMOS::Accounts::BuildNet->new($config, noacl => 1);
53my $labase = $base ? $LA->base($base) : $LA->default_base;
54$labase && $labase->load or die "Cannot load base";
55
56my @ips;
57foreach (@ARGV) {
58    my $ozone = $labase->get_object('netzone', $_) or die "Can't find zone $_";
59    push(@ips, grep { $_ } $ozone->get_attributes('freeIP'));
60}
61if (@ips) {
62    if ($first) {
63        print "$ips[0]\n";
64    } elsif ($random) {
65        print $ips[rand($#ips)] . "\n";
66
67    } else {
68        print join('', map { "$_\n" } @ips);
69    }
70} else {
71    warn "No free ip found\n";
72}
Note: See TracBrowser for help on using the repository browser.