-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify-subnet
executable file
·130 lines (100 loc) · 3.7 KB
/
modify-subnet
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to modify subnets in our database
#
use strict;
use Config::IniFiles;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_subnet $opt_ipver $opt_desc $opt_short_desc $opt_htmlcolor $opt_dhcpconfig $opt_owner $opt_profilelist $opt_force $opt_debug $opt_d $opt_h $opt_help);
my %o = ();
my $res = GetOptions (
"subnet=s",
"ipver=i",
"desc=s",
"short_desc=s",
"htmlcolor=s",
"dhcpconfig=s",
"owner=s",
"profilelist=s",
"force",
"debug",
"d",
"h",
"help"
);
my $debug = defined ($opt_d) || defined ($opt_debug);
my $subnet_name = shift;
my $hostdbini = Config::IniFiles->new (-file => HOSTDB::get_inifile ());
die ("$0: Config file access problem.\n") unless ($hostdbini);
my %colors = load_colors ($hostdbini);
if (! $subnet_name or $opt_h or $opt_help) {
# interpolation
my $inifilename = HOSTDB::get_inifile ();
my $colors = join ("\n ", sort keys %colors);
$colors = "Colors defined in $inifilename :\n $colors" if ($colors);
die(<<EOT);
Syntax: $0 [options] subnet
options:
-d debug
--force skip some sanity checks
All the following should be followed by the new value (--ipver 4) :
--subnet new subnet
--ipver IP version (currently useless)
--desc description
--short_desc short description
--htmlcolor color (see below)
--dhcpconfig dhcp config for subnet
--owner HOSTDB::Auth identifier
--profilelist comma-separated list of profiles for subnet
subnet is the subnet in slash notation (eg. 192.168.1.0/24)
color should be in html color syntax (eg. "#ffffcc") or
a color defined in $inifilename
$colors
EOT
}
my $hostdb = HOSTDB::DB->new (ini => $hostdbini, debug => $debug);
my $subnet = $hostdb->findsubnet ($subnet_name);
die ("Could not find subnet '$subnet_name' in database\n") if (! $subnet);
test_color ($hostdb, \%colors, $opt_htmlcolor) if (defined ($opt_htmlcolor));
$subnet->subnet ($opt_subnet) or die ("$0: Invalid subnet - $subnet->{error}\n") if (defined ($opt_subnet));
$subnet->ipver ($opt_ipver) or die ("$0: Invalid ipver - $subnet->{error}\n") if (defined ($opt_ipver));
$subnet->description ($opt_desc) or die ("$0: Invalid description - $subnet->{error}\n") if (defined ($opt_desc));
$subnet->short_description ($opt_short_desc) or die ("$0: Invalid short_description - $subnet->{error}\n") if (defined ($opt_short_desc));
$subnet->htmlcolor ($opt_htmlcolor) or die ("$0: Invalid htmlcolor - $subnet->{error}\n") if (defined ($opt_htmlcolor));
$subnet->dhcpconfig ($opt_dhcpconfig) or die ("$0: Invalid dhcpconfig - $subnet->{error}\n") if (defined ($opt_dhcpconfig));
$subnet->owner ($opt_owner) or die ("$0: Invalid owner - $subnet->{error}\n") if (defined ($opt_owner));
$subnet->profilelist ($opt_profilelist) or die ("$0: Invalid profilelist - $subnet->{error}\n") if (defined ($opt_profilelist));
$subnet->commit() or die ("Could not commit subnet object: " . $hostdb->{error} . "\n");
exit (0);
sub load_colors
{
my $hostdbini = shift;
my %res;
my @colors = $hostdbini->Parameters ('subnet_colors');
my $t;
foreach $t (@colors) {
$res{$t} = $hostdbini->val ('subnet_colors', $t);
}
# make RED the default so that a non-specified color is obvious
$res{default} = "#ff0000" if (! defined ($res{default}));
return %res;
}
sub test_color
{
my $hostdb = shift;
my $colors_ref = shift;
my $color_name = shift;
my $c = $color_name;
if (defined ($colors_ref->{$color_name})) {
$c = $colors_ref->{$color_name};
if (! $hostdb->is_valid_htmlcolor ($c)) {
die ("$0: Color '$color_name' resolved to '$c' which is an invalid HTML color.\n");
}
}
if (! $hostdb->is_valid_htmlcolor ($c)) {
warn ("$0: Color '$c' is an invalid HTML color.\n");
}
}