-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-host
executable file
·163 lines (136 loc) · 3.8 KB
/
delete-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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to delete hosts from the database
#
use strict;
use HOSTDB;
use Getopt::Long;
my %opt = ();
my $res = GetOptions (\%opt,
"id=i",
"ip=s",
"hostname=s",
"mac_address=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{ip})) {
die ("$0: Can't specify more than one search criteria at once (IP)\n") if ($search_for);
$datatype = 'IP';
$search_for = $opt{ip};
if (! $hostdb->is_valid_ip ($search_for)) {
die ("$0: Invalid IP address '$search_for'\n");
}
}
if (defined ($opt{hostname})) {
die ("$0: Can't specify more than one search criteria at once (hostname)\n") if ($search_for);
$datatype = 'FQDN';
$search_for = $opt{hostname};
if (! $hostdb->clean_hostname ($search_for)) {
die ("$0: Invalid hostname '$search_for'\n");
}
}
if (defined ($opt{mac_address})) {
die ("$0: Can't specify more than one search criteria at once (mac address)\n") if ($search_for);
$datatype = 'MAC';
$search_for = $opt{mac_address};
if (! $hostdb->clean_mac_addr ($search_for)) {
die ("$0: Invalid MAC address '$search_for'\n");
}
}
usage('') if (! $search_for);
my $host = get_host ($hostdb, $search_for, $datatype);
die ("$0: Could not find host object\n") unless ($host);
if (! $opt{quiet}) {
printf "%-7s %-7s %-16s %s\n", 'id', 'partof', 'ip', 'hostname';
printf "%-7s %-7s %-16s %s\n", $host->id (),
defined ($host->partof ())?$host->partof ():'-',
$host->ip (), $host->hostname ();
}
if (! $opt{force}) {
die ("$0: Dying, you have to delete with --force\n");
}
$host->delete ($opt{force}?'YES':'WELL, UHH') or die ("$0: Could not delete host object - $host->{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 host id
--ip IP
--hostname FQDN
--mac_address MAC address
EOT
}
sub get_host
{
my $hostdb = shift;
my $search_for = shift;
my $datatype = shift;
my @host_refs;
if ($datatype eq "IP") {
if ($hostdb->is_valid_ip ($search_for)) {
@host_refs = $hostdb->findhostbyip ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid IP address");
return undef;
}
} elsif ($datatype eq "FQDN") {
if ($hostdb->is_valid_fqdn ($search_for)) {
@host_refs = $hostdb->findhostbyname ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid FQDN");
return undef;
}
} elsif ($datatype eq "MAC") {
my $t = $search_for;
if ($hostdb->clean_mac_address ($t)) {
$search_for = $t;
@host_refs = $hostdb->findhostbymac ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid MAC address");
return undef;
}
} elsif ($datatype eq "ID") {
if ($search_for =~ /^\d+$/) {
@host_refs = $hostdb->findhostbyid ($search_for);
} else {
warn ("Search failed: '$search_for' is not a valid ID");
return undef;
}
} else {
warn ("Search failed: don't recognize datatype '$datatype'");
return undef;
}
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];
}