-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli-whois
executable file
·213 lines (168 loc) · 4.41 KB
/
cli-whois
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to search for IP addresses or mac addresses
#
use strict;
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d);
getopts ('hd');
my $debug = defined ($opt_d);
my @searchfor = @ARGV;
if ($#searchfor == -1 || $opt_h) {
die ("Syntax: $0 [-d] <ID/IP/FQDN/MAC> ...\n");
}
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my $first = 1;
while (@searchfor) {
my @host_refs;
my $search_for = shift @searchfor;
if (is_wildcard ($search_for)) {
@host_refs = HOSTDB::unique_id($hostdb->findhostbywildcardname ($search_for),
$hostdb->findhostbyaliaswildcardname ($search_for)
);
} else {
@host_refs = $hostdb->findhost ('guess', $search_for);
}
if ($hostdb->{error}) {
die ("$0: $hostdb->{error}\n");
}
if (@host_refs) {
foreach my $host (@host_refs) {
print ("Host :\n\n");
# interpolation
my $id = $host->id ();
my $parent = $host->partof ()?$host->partof ():"-";
my $ip = $host->ip ();
my $mac = $host->mac_address () || "n/a";
my $mac_ts = $host->mac_address_ts () || "no timestamp";
my $hostname = $host->hostname () || 'NULL';
my $comment = $host->comment () || 'NULL';
my $owner = $host->owner ();
my $dhcpstatus = $host->dhcpstatus ();
my $dhcpmode = $host->dhcpmode ();
my $dnsstatus = $host->dnsstatus ();
my $dnsmode = $host->dnsmode ();
my $ttl = $host->ttl () || 'default';
my $profile = $host->profile ();
my $zone = $host->dnszone () || 'NULL';
my $manual_zone = $host->manual_dnszone () || 'NULL';
my $aliases_info = '';
my @aliases = $host->init_aliases ();
if (@aliases) {
my @a = ("\n\tAliases :");
foreach my $alias (sort aliassort @aliases) {
my $a_name = $alias->aliasname ();
my $a_ttl = 'default';
my $a_comment = '';
my $a_status = '';
if ($alias->ttl ()) {
$a_ttl = $alias->ttl () . ' seconds';
}
if ($alias->comment ()) {
$a_comment = '(' . $alias->comment () . ')';
}
if ($alias->dnsstatus () eq 'DISABLED') {
$a_status = '(dns status DISABLED)';
}
push (@a, sprintf ("\t %-30s TTL %s %s %s", $a_name, $a_ttl, $a_comment, $a_status));
}
$aliases_info = join ("\n", @a) . "\n";
}
print (<<EOH);
ID $id
Parent $parent
---
DNS :
IP address $ip
Hostname $hostname
Zone $zone
Manual zone $manual_zone
TTL $ttl
Mode $dnsmode
Status $dnsstatus
$aliases_info
DHCP :
MAC address $mac (last seen $mac_ts)
Mode $dhcpmode
Status $dhcpstatus
General :
Profile $profile
Comment $comment
Owner $owner
EOH
my @attrs = $host->init_attributes ();
my $numattrs = scalar @attrs;
if ($numattrs > 0) {
print (<<EOH);
Host has $numattrs attributes :
EOH
my $lastsection = '';
foreach my $attr (sort attributesort @attrs) {
my $key = $attr->key ();
my $section = $attr->section ();
my $value = $attr->get ();
if ($section ne $lastsection) {
print ("\n [$section]\n");
$lastsection = $section;
}
printf (" %-25s = %s\n", $key, $value);
}
print ("\n");
}
my $subnet = $hostdb->findsubnetbyip ($host->ip ());
if ($subnet) {
printf (" %-23s %-20s %s\n", "subnet", "netmask", "description");
printf " %-23s %-20s %s\n", $subnet->subnet(),
$subnet->netmask (), $subnet->description ();
print ("\n");
} else {
print ("Could not find a subnet in database\n\n");
}
}
print ("---\n\n");
} else {
warn ("$0: No match on '$search_for'\n");
}
}
sub attributesort
{
my $a_section = $a->section ();
my $b_section = $b->section ();
if ($a_section eq $b_section) {
my $a_key = $a->key ();
my $b_key = $b->key ();
if ($a_key =~ /^(.*?)(\d+)$/) {
my $a_prefix = $1;
my $a_num = int ($2);
if ($b_key =~ /^(.*?)(\d+)$/) {
my $b_prefix = $1;
my $b_num = int ($2);
if ($a_prefix eq $b_prefix) {
# both keys begin with the same text and ends in digits,
# do numeric comparision
return $a_num <=> $b_num;
}
}
}
return $a_key cmp $b_key;
}
return $a_section cmp $b_section;
}
sub aliassort
{
my $a_name = $a->aliasname ();
my $b_name = $b->aliasname ();
$a_name cmp $b_name;
}
sub is_wildcard
{
my $in = shift;
return 1 if ($in =~ /%/);
return 1 if ($in =~ /\*/);
return 0;
}