-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify-host
executable file
·194 lines (160 loc) · 6.59 KB
/
modify-host
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to modify hosts in the database
#
use strict;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_ip $opt_hostname $opt_manual_dnszone $opt_dnszone $opt_owner $opt_dhcpmode $opt_dhcpstatus $opt_dnsmode $opt_dnsstatus $opt_mac_address $opt_ttl $opt_comment $opt_partof $opt_profile $opt_debug $opt_force);
my %o = ();
my $res = GetOptions (
"ip=s",
"hostname=s",
"dnszone=s",
"manual_dnszone=s",
"owner=s",
"dhcpmode=s",
"dhcpstatus=s",
"dnsmode=s",
"dnsstatus=s",
"mac_address=s",
"ttl=s",
"comment=s",
"partof=i",
"profile=s",
"debug",
"force"
);
#die ("$0: Parsing options failed\n") if ($res);
my $debug = defined ($opt_debug);
my $search_for = shift;
sub usage
{
my $msg = shift;
# interpolation
die(<<EOT);
${msg}Syntax: $0 [options] <IP/FQDN/MAC/ID>
options:
--debug debug
--force well, force
object modifiers :
--ip IP address
--hostname FQDN
--dnszone The DNS zone to include this host in
--manual_dnszone Set to 'N' to disable automatic dnszone maintenance
--partof n parent Host ID
--dhcpmode STATIC or DYNAMIC
--dhcpstatus ENABLED or DISABLED
--dnsmode A_AND_PTR or A
--dnsstatus ENABLED or DISABLED
--mac_address MAC address
--ttl DNS TTL
--comment Comment abou host
--owner Username of owner
--profile Profile this host belongs to (DHCP config)
EOT
}
usage("") if (! $search_for);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my $host = get_host ($hostdb, 'guess', $search_for);
die ("$0: Could not find host object\n") unless ($host);
if (! $opt_force) {
# do some extra sanity checks if not forced
my $h;
my $ip = $opt_ip || $host->ip ();
my $subnet = $hostdb->findsubnetbyip ($ip);
if (! defined ($subnet)) {
die ("$0: No subnet matches IP '$ip', use --force to ignore.");
}
if ($opt_ip) {
$h = $hostdb->findhostbyip ($opt_ip);
die ("$0: A host with IP $opt_ip already exists, use --force to set IP anyways.\n") if (defined ($h));
}
if ($opt_hostname) {
$h = $hostdb->findhostbyname ($opt_hostname);
die ("$0: A host with hostname $opt_hostname already exists, use --force to set hostname anyways.\n") if (defined ($h));
}
if ($opt_mac_address) {
$h = $hostdb->findhostbymac ($opt_mac_address);
die ("$0: A host with MAC address $opt_mac_address already exists, use --force to set MAC address anyways.\n") if (defined ($h));
}
if ($opt_partof) {
$h = $hostdb->findhostbyid ($opt_partof);
die ("$0: A parent host with ID $opt_partof could not be found, use --force to set partof anyways.\n") unless (defined ($h));
}
if (defined ($opt_profile) and (uc ($opt_profile) ne 'NULL' and $opt_profile)) {
my @profiles = split (',', $subnet->profilelist ());
if (! grep (/^$opt_profile$/, @profiles)) {
die ("$0: No profile called '$opt_profile' for subnet " . $subnet->subnet () . ", use --force to set profile anyways.\n");
}
}
if ($opt_dnszone) {
my $is_manual = 0;
$is_manual = 1 if (defined ($opt_manual_dnszone) and $opt_manual_dnszone eq 'Y');
$is_manual = 1 if ($host->manual_dnszone () eq 'Y');
die ("$0: When setting dnszone, you probaby want to set manual_dnszone to 'Y' too (if you are creating glue records). Use --force to override.\n") unless ($is_manual);
}
}
my $set_profile = 'default';
if ($opt_profile) {
$set_profile = $opt_profile unless (uc ($opt_profile) eq 'NULL' or ! $opt_profile);
}
$host->ip ($opt_ip) or die ("$0: Invalid ip - $host->{error}\n") if (defined ($opt_ip));
$host->owner ($opt_owner) or die ("$0: Invalid owner - $host->{error}\n") if (defined ($opt_owner));
$host->dhcpmode ($opt_dhcpmode) or die ("$0: Invalid dhcpmode - $host->{error}\n") if (defined ($opt_dhcpmode));
$host->dhcpstatus ($opt_dhcpstatus) or die ("$0: Invalid dhcpstatus - $host->{error}\n") if (defined ($opt_dhcpstatus));
$host->dnsmode ($opt_dnsmode) or die ("$0: Invalid dnsmode - $host->{error}\n") if (defined ($opt_dnsmode));
$host->dnsstatus ($opt_dnsstatus) or die ("$0: Invalid dnsstatus - $host->{error}\n") if (defined ($opt_dnsstatus));
$host->mac_address ($opt_mac_address) or die ("$0: Invalid mac_address - $host->{error}\n") if (defined ($opt_mac_address));
$host->ttl ($opt_ttl) or die ("$0: Invalid ttl - $host->{error}\n") if (defined ($opt_ttl));
$host->comment ($opt_comment) or die ("$0: Invalid comment - $host->{error}\n") if (defined ($opt_comment));
$host->partof ($opt_partof) or die ("$0: Invalid partof - $host->{error}\n") if (defined ($opt_partof));
$host->manual_dnszone ($opt_manual_dnszone) or die ("$0: Invalid manual_dnszone - $host->{error}\n") if (defined ($opt_manual_dnszone));
$host->profile ($set_profile) or die ("$0: Invalid profile - $host->{error}\n") if (defined ($opt_profile));
$hostdb->clean_hostname($opt_hostname) if (defined ($opt_hostname));
if (defined ($opt_dnszone)) {
$host->dnszone ($opt_dnszone) or die ("$0: Invalid dnszone - $host->{error}\n");
} else {
if (defined ($opt_hostname) and $opt_hostname ne $host->hostname()) {
die ("$0: Invalid FQDN '$opt_hostname'\n") unless ($opt_force or $hostdb->is_valid_fqdn ($opt_hostname));
# setting hostname, but not setting DNS zone. auto-detect.
my $z = $hostdb->findzonebyhostname ($opt_hostname);
if (defined ($z)) {
if ($host->dnszone () ne $z->zonename()) {
if ($host->manual_dnszone () ne 'Y') {
warn ("$0: You changed hostname so I changed DNS zone for you (from " . $host->dnszone () . " to " . $z->zonename () . ")\n");
$host->dnszone ($z->zonename ());
} else {
warn ("$0: NOT changing DNS zone (from " . $host->dnszone () . " to " . $z->zonename () . ") since manual_dnszone is set to 'Y' for this host\n");
}
}
} else {
warn ("$0: Could not find a suitable dnszone for new hostname '$opt_hostname'\n");
}
}
}
$host->hostname ($opt_hostname) or die ("$0: Invalid hostname - $host->{error}\n") if (defined ($opt_hostname));
$host->commit () or die ("$0: Could not commit host object - $host->{error}\n");
exit (0);
sub get_host
{
my $hostdb = shift;
my $datatype = shift;
my $search_for = shift;
my @host_refs;
@host_refs = $hostdb->findhost ($datatype, $search_for);
if ($#host_refs == -1) {
warn ("$0: Search for '$search_for' (type '$datatype') failed - no match\n");
return undef;
}
if ($#host_refs == -1) {
my $count = $#host_refs + 1;
warn ("$0: Search for '$search_for' (type '$datatype') failed - more than one ($count) match\n");
return undef;
}
return $host_refs[0];
}