Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 164 lines (136 sloc) 3.8 KB
#!/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];
}
You can’t perform that action at this time.