-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-hostattr
executable file
·88 lines (68 loc) · 1.9 KB
/
delete-hostattr
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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to set host attributes
#
use strict;
use HOSTDB;
use Getopt::Long;
use vars qw ($opt_debug $opt_help $opt_force);
my $res = GetOptions (
"debug",
"help",
"force"
);
my $force = defined ($opt_force);
my $debug = defined ($opt_debug);
if ($#ARGV != 2 || $opt_help) {
die (<<EOH);
Syntax: $0 [options] <ID/IP/FQDN/MAC> attribute section
Options :
--debug debug
--force force, really do what you want
EOH
}
my $searchfor = shift;
my $attribute = shift;
my $section = shift;
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @host_refs = $hostdb->findhost ('guess', $searchfor);
if ($hostdb->{error}) {
die ("$0: $hostdb->{error}\n");
}
if (@host_refs) {
if (scalar @host_refs != 1) {
my @hl;
foreach my $host (@host_refs) {
my $id = $host->id ();
my $ip = $host->ip () || 'no ip';
my $hn = $host->hostname () || 'no hostname';
my $mac = $host->mac_address () || 'no mac address';
push (@hl, "id $id, $ip, $hn, $mac");
}
die ("$0: Host match pattern '$searchfor' is ambigous. Hosts matching :\n " . join ("\n ", @hl) . "\n");
}
my $host = shift @host_refs;
$host->init_attributes ();
my $attr = $host->get_attribute ($attribute, $section);
my $old_value = 'none';
$old_value = $attr->get () if (defined ($attr));
print ("Found attribute $attr (value '$old_value')\n") if ($debug);
if (defined ($attr)) {
if (! $opt_force) {
die ("$0: Dying, you have to delete with --force\n");
}
$attr->delete ($opt_force?"YES":"WELL, UHH") or die ("$0: Could not delete host attribute object - $attr->{error}\n");
} else {
my $id = $host->id ();
my $ip = $host->ip () || 'no ip';
my $hn = $host->hostname () || 'no hostname';
die ("$0: Host with ID $id (name $hn, IP $ip) has no such attribute\n");
}
exit (0);
} else {
die ("$0: No host matches '$searchfor'\n");
}