-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-host
executable file
·143 lines (121 loc) · 5.68 KB
/
add-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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to add hosts to the database
#
use strict;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_dhcpmode $opt_dhcpstatus $opt_dnsmode $opt_dnsstatus $opt_mac_address $opt_ttl $opt_comment $opt_partof $opt_dnszone $opt_manual_dnszone $opt_debug $opt_force $opt_help);
my %o = ();
my $res = GetOptions (
"dhcpmode=s",
"dhcpstatus=s",
"dnsmode=s",
"dnsstatus=s",
"mac_address=s",
"ttl=i",
"comment=s",
"partof=i",
"dnszone=s",
"manual_dnszone=s",
"debug",
"force",
"help"
);
#die ("$0: Parsing options failed\n") if ($res);
my $debug = defined ($opt_debug);
my $ip = shift;
my $hostname = shift;
my $owner = shift;
sub usage
{
my $msg = shift;
# interpolation
die(<<EOT);
${msg}Syntax: $0 [options] ip hostname owner
options:
--debug debug
--force don\'t perform certain sanity checks (DUP checking)
object modifiers :
--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 about host
--partof parent Host ID
--dnszone Which zone to include this host record in (defaults to the most logical one)
--manual_dnszone Set to 'N' to disable automatic dnszone maintenance
EOT
}
usage ('') if ($opt_help);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
usage ("") if (! defined ($ip) or ! defined ($hostname) or ! defined ($owner));
usage ("Invalid IP address '$ip'\n\n") unless ($hostdb->is_valid_ip ($ip));
if (! ($opt_dnsstatus?$opt_dnsstatus:'' eq 'DISABLED' and $hostname eq 'NULL')) {
# if dnsstatus is DISABLED, an empty hostname is valid
if (! $opt_force) {
$hostdb->clean_hostname ($hostname);
usage ("Invalid FQDN '$hostname'\n\n") unless ($hostdb->is_valid_fqdn ($hostname));
} else {
usage ("Invalid hostname '$hostname'\n\n") unless ($hostdb->clean_hostname ($hostname));
}
}
if (! $opt_force) {
# do some extra sanity checks if not forced
my $h = $hostdb->findhostbyip ($ip);
die ("$0: A host with IP $ip already exists, use --force to add anyways.\n") if (defined ($h));
$h = $hostdb->findhostbyname ($hostname);
die ("$0: A host with hostname $hostname already exists, use --force to add anyways.\n") if (defined ($h));
$h = $hostdb->findhostbyaliasname ($hostname);
die ("$0: A host with alias name $hostname already exists, use --force to add 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 add anyways.\n") if (defined ($h));
}
if ($opt_partof) {
$h = $hostdb->findhostbyid ($opt_partof);
die ("$0: A parent host with ID $opt_mac_address could not be found, use --force to set partof anyways.\n") if (defined ($h));
}
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 ($h->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 $host = $hostdb->create_host ();
$host->ip ($ip) or die ("$0: Invalid value - $host->{error}\n");
$host->hostname ($hostname) or die ("$0: Invalid value - $host->{error}\n");
$host->owner ($owner) or die ("$0: Invalid value - $host->{error}\n");
# defaults, set before checking if they are supplied
$host->dhcpmode ('STATIC') or die ("$0: Could not set default 'dhcpmode': $host->{error}\n");
$host->dhcpstatus ('ENABLED') or die ("$0: Could not set default 'dhcpstatus': $host->{error}\n");
$host->dnsmode ('A_AND_PTR') or die ("$0: Could not set default 'dhcpmode': $host->{error}\n");
$host->dnsstatus ('ENABLED') or die ("$0: Could not set default 'dnsstatus': $host->{error}\n");
$host->manual_dnszone('N') or die ("$0: Could not set default 'manual_dnszone': $host->{error}\n");
$host->profile('default') or die ("$0: Could not set default 'profile': $host->{error}\n");
# optional ones
$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));
if (! defined ($opt_dnszone)) {
my $z = $hostdb->findzonebyhostname ($host->hostname ());
die ("$0: Could not find a suitable DNS zone for hostname '" . $host->hostname () . "'\n") unless defined ($z);
$host->dnszone ($z->zonename ()) or die ("$0: Invalid dnszone - $host->{error}\n");
} else {
$host->dnszone ($opt_dnszone) or die ("$0: Invalid dnszone - $host->{error}\n");
}
$host->commit () or die ("$0: Could not commit host object - $host->{error}\n");
exit (0);