source: trunk/LATMOS-Accounts/bin/la-sql-freeip @ 2380

Last change on this file since 2380 was 1197, checked in by nanardon, 11 years ago

rename la-free-ip to la-sql-freeip since it's a pure sql base command

  • Property svn:executable set to *
File size: 1.3 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
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
52@ARGV or pod2usage(1);
53
54my $LA = LATMOS::Accounts->new($config, noacl => 1);
55my $labase = $LA->base($base);
56$labase && $labase->load or die "Cannot load base";
57
58my @ips;
59foreach (@ARGV) {
60    my $ozone = $labase->get_object('netzone', $_) or die "Can't find zone $_";
61    push(@ips, grep { $_ } $ozone->get_attributes('freeIP'));
62}
63if (@ips) {
64    if ($first) {
65        print "$ips[0]\n";
66    } elsif ($random) {
67        print $ips[rand($#ips)] . "\n";
68
69    } else {
70        print join('', map { "$_\n" } @ips);
71    }
72} else {
73    warn "No free ip found\n";
74}
Note: See TracBrowser for help on using the repository browser.