-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnir
executable file
·466 lines (388 loc) · 10.6 KB
/
nir
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env perl
##
## Mr. Netops's nifty inline resolver
## Copyright 2013 Todd Jimenez - [email protected]
## https://github.com/mrnetops/nir
##
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License version 2
## as published by the Free Software Foundation
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see
## <http://www.gnu.org/licenses/gpl-2.0.html>.
##
use Socket qw(:all);
use Getopt::Long;
use Data::Dumper;
use strict;
use warnings;
$| = 1;
##
## nasty way to address ipv6 Socket constants not being present in older
## Socket libraries
##
no strict "subs";
%::commandLineOptions = (
##
## Per http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address
##
## with some regexp bug fixes
## * reversed the ip address octet subregexp to ensure we do a greedy
## match on the last octet otherwise we only match up to the first digit
## of the last octet.
## * added + to the [A-Za-z] hostname subregexp to ensure we do greedy
## matching on the top level domain otherwise we only match up to the
## first letter of the top level domain.
## * escape "." to prevent it doing an any character match instead of a
## literal "." match
##
'ipV4AddressRegexp' =>
'((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])',
##
## It's suprising difficult to find a good ipv6 regexp
## Massaged from Regexp::IPv6
##
'ipV6AddressRegexp' =>
'(?^::(:[0-9a-fA-F]{1,4}){0,5}((:[0-9a-fA-F]{1,4}){1,2}|:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})))|[0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}:([0-9a-fA-F]{1,4}|:)|(:([0-9a-fA-F]{1,4})?|((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))))|:(((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})?|))|(:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|:[0-9a-fA-F]{1,4}(:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|(:[0-9a-fA-F]{1,4}){0,2})|:))|((:[0-9a-fA-F]{1,4}){0,2}(:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|(:[0-9a-fA-F]{1,4}){1,2})|:))|((:[0-9a-fA-F]{1,4}){0,3}(:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|(:[0-9a-fA-F]{1,4}){1,2})|:))|((:[0-9a-fA-F]{1,4}){0,4}(:((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))|(:[0-9a-fA-F]{1,4}){1,2})|:)))',
'hostnameRegexp' =>
"(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\\.)+([A-Za-z]+|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])",
#"(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\\.)*([A-Za-z]+|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])",
'format' =>
'[%s]',
'separator' =>
',',
'tags' =>
0,
'timeout' =>
5,
);
##
## Perl 5.14.0 or newer is required for IPv6 support
## Unless Socket is manually updated
##
eval {
require 5.14.0;
};
if ($@) {
$::commandLineOptions{ipV6Support} = 0;
} else {
$::commandLineOptions{ipV6Support} = 1;
}
@::getOptionsOptions = (
\%::commandLineOptions,
'ipV4AddressRegexp=s',
'hostnameRegexp=s',
'ipV6Support!',
'format=s',
'separator=s',
'tags!',
'verbose!',
'debug!',
'timeout=i',
'lookup=s@',
'help|h|?',
);
if (exists $ENV{'NIR'}) {
@ARGV = (
split(' ', $ENV{NIR}), @ARGV
);
}
GetOptions(
@::getOptionsOptions
) or exit 1;
##
## GetOptions arrays are additive so set $::commandLineOptions{lookup}
## conditionally AFTER calling GetOptions, otherwise we could never use
## a subset of the default, only a superset.
##
unless (exists $::commandLineOptions{lookup}) {
$::commandLineOptions{lookup} = [
'hostname', 'ipv4', 'ipv6', 'canonname'
],
}
$::commandLineOptions{lookup} = [
split(
/,/,
join(
',',
@{ $::commandLineOptions{lookup} }
)
)
];
if (exists $::commandLineOptions{help}) {
print "
Mr. Netops's nifty inline resolver
Options:
-format <format>
Specify alternate formatting for resolved values
-separator <separator>
Specify alternate separator for lookups with multiple values
-tags
Add type tags to resolved values
-verbose
Print additional status information to stderr
-debug
Print debugging information to stderr
-timeout <timeout>
Dns lookup timeout
-lookup <types>
Types of resolved values and their presentation order
Supports The following types: hostname, ipv4, ipv6, canonname
-help
This
Advanced Options:
-hostnameRegexp <regexp>
Specify an alternate hostname regular expression
-ipV4AddressRegexp <regexp>
Specify an alternate ipv4 address regular expression
-ipV6AddressRegexp <regexp>
Specify an alternate ipv6 address regular expression
-ipV6Support
Manually enable ipv6 support
-noipV6Support
Manually disable ipv6 support
Examples:
Basic invocation
someCommand | nir
Present ipv4 & ipv6 resolved values in that order
someCommand | nir -lookup ipv4,ipv6
Format inline lookups in curly braces and seperate values with ;
someCommand | nir -format '{%s}' -separator ';'
Use Cases:
dig facebook.com ANY | nir
mtr -wr facebook.com | nir
cat /etc/hosts | nir
arp -a | nir
";
exit 0;
}
if (exists $::commandLineOptions{debug}) {
$Data::Dumper::Varname = 'Configuration';
$Data::Dumper::Sortkeys = 1;
&debug(
Dumper(\%::commandLineOptions)
);
}
if ($::commandLineOptions{ipV6Support}) {
$::masterRegexp = join(
"|",
$::commandLineOptions{'ipV4AddressRegexp'},
$::commandLineOptions{'ipV6AddressRegexp'},
$::commandLineOptions{'hostnameRegexp'}
);
} else {
$::masterRegexp = join(
"|",
$::commandLineOptions{'ipV4AddressRegexp'},
$::commandLineOptions{'hostnameRegexp'}
);
}
while (<>) {
$_ =~ s/($::masterRegexp)/&resolveMe($1)/ge;
print $_;
}
sub resolveMe {
my ($string) = @_;
if (exists $::cache{$string}) {
return $::cache{$string}{'formatted'};
}
if ($string =~ m/$::commandLineOptions{'hostnameRegexp'}/) {
&resolveMe_hostname($string);
} else {
&resolveMe_ipAddress($string);
}
&format($string);
return $::cache{$string}{'formatted'};
}
sub resolveMe_hostname {
if ($::commandLineOptions{ipV6Support}) {
&resolveMe_getStarInfo('hostname', @_);
} else {
&resolveMe_gethostbyname(@_);
}
}
sub resolveMe_ipAddress {
if ($::commandLineOptions{ipV6Support}) {
&resolveMe_getStarInfo("ip", @_);
} else {
&resolveMe_gethostbyaddr(@_);
}
}
sub resolveMe_gethostbyname {
my ($string) = @_;
&debug("gethostbyname [$string]");
my @addresses = gethostbyname($string);
if (@addresses) {
@addresses = map {
inet_ntoa($_)
} @addresses[4 .. $#addresses];
if ($addresses[0] ne $string) {
foreach my $address (@addresses) {
++$::cache{
$string
}{
'resolved'
}{
'ipv4'
}{
$address
};
}
}
}
}
sub resolveMe_gethostbyaddr {
my ($string) = @_;
&debug("gethostbyaddr [$string]");
my $name;
eval {
alarm(0);
local $SIG{ALRM} = sub { die; };
alarm($::commandLineOptions{'timeout'});
my $inet_aton = inet_aton($string);
if (defined $inet_aton) {
$name = gethostbyaddr($inet_aton, AF_INET);
}
alarm(0);
};
if ($@) {
&verbose("gethostbyaddr timeout [$string]");
return $::cache{$string};
}
if (defined $name) {
++$::cache{$string}{'resolved'}{'hostname'}{$name};
}
}
##
## getaddrinfo / getnameinfo lookups
##
sub resolveMe_getStarInfo {
my ($type, $string) = @_;
my %hints = (
flags => AI_CANONNAME,
);
&debug("getaddrinfo [$string]");
my ($error, @addresses) = getaddrinfo(
$string,
undef,
\%hints
);
my %lookup;
my %information;
my $flags = $type eq 'hostname' ? NI_NUMERICHOST : 0;
foreach my $address (@addresses) {
if (exists $lookup{ $address->{addr} }) { next; }
++$lookup{ $address->{addr} };
if (
$address->{canonname}
) {
++$::cache{$string}{'resolved'}{'canonname'}{
$address->{canonname}
};
}
my ($err, $hostname);
eval {
alarm(0);
local $SIG{ALRM} = sub { die; };
alarm($::commandLineOptions{'timeout'});
($err, $hostname) = getnameinfo(
$address->{addr}, $flags
);
alarm(0);
};
if ($@ || $err) {
&verbose("getnameinfo error [$string]: [$@$err]");
} else {
if ($type eq 'hostname') {
if ($address->{family} == AF_INET) {
++$::cache{
$string
}{
'resolved'
}{
'ipv4'
}{
$hostname
};
}
if ($address->{family} == AF_INET6) {
++$::cache{
$string
}{
'resolved'
}{
'ipv6'
}{
$hostname
};
}
} else {
++$::cache{
$string
}{
'resolved'
}{
'hostname'
}{
$hostname
};
}
}
}
}
sub format {
my ($string) = @_;
unless(exists $::cache{$string}{'resolved'}) {
$::cache{$string}{'formatted'} = $string;
return;
}
my @elements;
foreach my $type (
@{ $::commandLineOptions{'lookup'} }
) {
unless (exists $::cache{$string}{'resolved'}{$type}) { next; }
foreach my $address (
keys %{
$::cache{$string}{'resolved'}{$type}
}
) {
if ($string eq $address) { next; }
if ($::commandLineOptions{'tags'}) {
push(@elements, "${type}:${address}");
} else {
push(@elements, $address);
}
}
}
if (@elements) {
$::cache{$string}{'formatted'} = $string . sprintf(
$::commandLineOptions{'format'},
join($::commandLineOptions{'separator'}, @elements)
);
} else {
$::cache{$string}{'formatted'} = $string;
}
}
sub verbose {
my ($message) = @_;
if (
exists $::commandLineOptions{'verbose'}
) {
print STDERR "VERBOSE: $message\n";
}
}
sub debug {
my ($message) = @_;
if (
exists $::commandLineOptions{'debug'}
) {
print STDERR "DEBUG: $message\n";
}
}