-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify-hostalias
executable file
·119 lines (93 loc) · 3.04 KB
/
modify-hostalias
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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to modify host aliases in the database
#
use strict;
use HOSTDB;
use Getopt::Long;
my %opt = ();
my $res = GetOptions (\%opt,
'aliasname=s',
'dnsstatus=s',
'ttl=s',
'comment=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] <ID/FQDN>
options:
--debug debug
--force well, force
object modifiers :
--aliasname FQDN
--dnsstatus ENABLED or DISABLED
--ttl DNS TTL
--comment Comment abou host
EOT
}
usage('') if (! $search_for);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my $alias = get_alias ($hostdb, $search_for);
die ("$0: Could not find host alias object\n") unless ($alias);
if (! $opt{force}) {
# do some extra sanity checks if not forced
if ($opt{aliasname}) {
my @h = $hostdb->findhost ('guess', $opt{aliasname});
if (@h) {
my $id = $h[0]->id ();
my $name = $h[0]->hostname ();
die ("$0: At least one host ($name, id $id) matching that name ($opt{aliasname}) already exists\n");
}
}
}
$alias->dnsstatus ($opt{dnsstatus}) or die ("$0: Invalid dnsstatus - $alias->{error}\n") if (defined ($opt{dnsstatus}));
$alias->ttl ($opt{ttl}) or die ("$0: Invalid ttl - $alias->{error}\n") if (defined ($opt{ttl}));
$alias->comment ($opt{comment}) or die ("$0: Invalid comment - $alias->{error}\n") if (defined ($opt{comment}));
if (defined ($opt{aliasname})) {
# setting aliasname. also set dns zone.
my $z = $hostdb->findzonebyhostname ($opt{aliasname});
die ("$0: Could not find a suitable dnszone for new aliasname '$opt{aliasname}'\n") unless ($z);
my $zonename = $z->zonename ();
$alias->aliasname ($opt{aliasname}) or die ("$0: Invalid aliasname - $alias->{error}\n");
$alias->dnszone ($zonename) or die ("$0: Invalid zonename - $alias->{error}\n");
# CNAMEs can't co-exist with any other record in DNS
die ("Can't have an alias with the same name as a zone ($aliasname)\n") if ($hostdb->findzonebyname ($aliasname));
}
$alias->commit () or die ("$0: Could not commit host alias object - $alias->{error}\n");
exit (0);
sub get_alias
{
my $hostdb = shift;
my $search_for = shift;
my @alias_refs;
if ($search_for =~ /^\d+$/) {
# all numeric, look for alias using id
@alias_refs = $hostdb->findhostaliasbyid ($search_for);
} else {
die ("$0: '$search_for' is not a valid hostname (and it does not appear to be an alias ID)!\n")
unless ($hostdb->clean_hostname ($search_for));
@alias_refs = $hostdb->findhostaliasbyname ($search_for);
}
if (! @alias_refs) {
warn ("$0: Search for '$search_for' failed - no match\n");
return undef;
}
if (0 + @alias_refs != 1) {
my $count = 0 + @alias_refs;
warn ("$0: Search for '$search_for' failed - more than one ($count) match\n");
return undef;
}
return $alias_refs[0];
}