-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipso_block
executable file
·107 lines (98 loc) · 3.04 KB
/
ipso_block
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
#!/usr/bin/perl
use strict;
use warnings;
use lib './lib';
use Ipso;
use Getopt::Long::Descriptive;
use Text::ASCIITable;
use Time::Duration;
use Switch;
my ($opt,$usage)=describe_options("%c %o",
['Action:'],
["action"=>hidden=>{one_of=>[
["add|a=s" => "Add an IPv4 or IPv6 network block."],
["list|l" => "List network blocks present."],
["delete|d=s" => "Delete a network block."],
["map|m=s" => "Map IP allocations in a network block."],
]}],
[],
['Parameters:'],
['note|n=s' => 'Some descriptive text for the block.'],
[],
['Other:'],
['help|h' => 'Print help and usage.'],
[],
['Examples:'],
["$0 -a 192.168.0.0/16 -n 'Some RFC1918 space.'"],
["$0 --add dead:beef::/64 --note 'An IPv6 block.'"],
["$0 -l"],
["$0 -d dead:beef::/64"],
[],
);
print($usage->text), exit if $opt->help;
my $action=$opt->action;
my $note=$opt->note || '';
my $block;
switch ($action){
case 'add' { $block=$opt->add; addblock(); }
case 'list' { listblocks(); }
case 'delete' { $block=$opt->delete; deleteblock(); }
case 'map' { $block=$opt->map; mapblock(); }
default { print "Error: Unknown or no action requested.\n"; exit; }
}
sub addblock{
unless (is_ipblock_identifier($block)){
print "Error: Invalid or no network block supplied.\n";
exit;
}
if ($note eq ''){
print "Error: No descriptive text supplied.\n";
exit;
}
addIPBlock($block,$note);
}
sub listblocks{
my %blocks=getBlockInfo();
my $table = Text::ASCIITable->new({ headingText => 'Available IP Blocks' });
$table->setCols('ID','Block','Allocations','Used (%)','Free (%)','Notes','Last updated about...');
foreach my $this (sort {$a<=>$b} keys %blocks){
my $block=$blocks{$this}{ipblock};
my $allocations=$blocks{$this}{allocations} || 0;
my $maxips=$blocks{$this}{maxips};
my $used=$blocks{$this}{allocated} || 0;
my $note=$blocks{$this}{note};
my $age=ago($blocks{$this}{age},1);
my $free=$maxips-$used;
my $usedpcent='0';
if ($used!=0){
$usedpcent=sprintf("%.3g",$used/$maxips*100);
}
my $freepcent=sprintf("%.3g",100-$usedpcent);
$table->addRow($this,$block,$allocations,"$used ($usedpcent%)","$free ($freepcent%)",$note,$age);
}
print $table;
}
sub deleteblock{
unless (is_ipblock_identifier($block)){
print "Error: $block is not a valid IP block identifier.\n";
exit;
}
deleteIPBlock($block);
}
sub mapblock{
unless (is_ipblock_identifier($block)){
print "Error: $block is not a valid IP block identifier.\n";
exit;
}
my %map=mapIPBlock($block);
my %blockinfo=getBlockInfo();
my $table = Text::ASCIITable->new({ headingText => "Allocations map for block $blockinfo{$block}{ipblock}" });
$table->setCols('Allocation Start','Allocation End','No IPs','Note');
$table->alignCol('Allocation Start','left');
$table->alignCol('Allocation End','left');
foreach my $chunk (sort keys %map){
if ($map{$chunk}{firstip} eq $map{$chunk}{lastip}){$map{$chunk}{firstip}='-';}
$table->addRow($map{$chunk}{firstip},$map{$chunk}{lastip},$map{$chunk}{ipcount},$map{$chunk}{note});
}
print $table;
}