-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-hostalias
executable file
·117 lines (92 loc) · 2.46 KB
/
delete-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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to delete host aliases from the database
#
use strict;
use HOSTDB;
use Getopt::Long;
my %opt = ();
my $res = GetOptions (\%opt,
'id=i',
'aliasname=s',
'debug',
'force',
'quiet'
);
#die ("$0: Parsing options failed\n") if ($res);
my ($search_for, $datatype);
my $debug = defined ($opt{debug});
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
if (defined ($opt{id})) {
$datatype = 'ID';
$search_for = $opt{id};
}
if (defined ($opt{aliasname})) {
die ("$0: Can't specify more than one search criteria at once (aliasname)\n") if ($search_for);
$datatype = 'FQDN';
$search_for = $opt{aliasname};
if (! $hostdb->clean_hostname ($search_for)) {
die ("$0: Invalid hostname '$search_for'\n");
}
}
usage('') if (! $search_for);
my $alias = get_alias ($hostdb, $search_for, $datatype);
die ("$0: Could not find alias object\n") unless ($alias);
if (! $opt{quiet}) {
printf " %-7s %-25s %s\n", 'id', 'aliasname', 'hostname';
my $h = $hostdb->findhostbyid ($alias->hostid ());
my $hostname = 'NOT FOUND';
if ($h) {
$hostname = $h->hostname () . ' (id ' . $h->id () . ')';
}
printf " %-7s %-25s %s\n", $alias->id (), $alias->aliasname (), $hostname;
}
if (! $opt{force}) {
die ("$0: Dying, you have to delete with --force\n");
}
$alias->delete ($opt{force}?'YES':'WELL, UHH') or die ("$0: Could not delete host object - $alias->{error}\n");
print ("Host object deleted\n") if (! $opt{quiet});
exit (0);
sub usage
{
my $msg = shift;
# interpolation
die(<<EOT);
${msg}Syntax: $0 datatype search_for
options:
--debug debug
--force well, force
--quiet quiet
datatypes:
--id alias id
--aliasname FQDN
EOT
}
sub get_alias
{
my $hostdb = shift;
my $search_for = shift;
my $datatype = shift;
my @alias_refs;
if ($datatype eq 'ID') {
@alias_refs = $hostdb->findhostaliasbyid ($search_for);
} elsif ($datatype eq 'FQDN') {
@alias_refs = $hostdb->findhostaliasbyname ($search_for);
} else {
die ("$0: Invalid datatype passed to get_alias () : $datatype\n");
}
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];
}