-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmdnsscan
executable file
·103 lines (96 loc) · 2.25 KB
/
mdnsscan
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
#!/usr/bin/perl
#
# find all mDNS hosts on a network quickly
# - scan for dns-sd discoverable & ipp services
# - lookup mdns host for the discovered services
# - get ip info for the mdns hosts
#
# -samy kamkar 2025/01/13
use strict;
my $DEBUG = shift;
my $DNSSD = "dns-sd";
my @SCAN = ("_services._dns-sd._udp", "_ipp._tcp");
my %SCAN = map { $_ => 1 } @SCAN;
my $STYPE = "Service Type";
my $DOMAIN = "local.";
my (%services, %lookup, %scanned, %hosts);
scan($_, 2) for @SCAN;
for my $service (keys %services)
{
#print "services $_\n";
scan($service, 1) unless $scanned{$service}++;
lookup($_, $lookup{$_}) for keys %lookup;
}
#########################################################################
#
sub scan
{
my ($s, $t) = @_;
my $cmd = "$DNSSD -B '$s' $DOMAIN";
my ($slen, $ilen, $nlen);
runt($cmd, sub {
#print " > $_ ($slen)";
# find our columns
if (!$slen)
{
if (/$STYPE(\s+)/)
{
$slen = index($_, $STYPE);
$nlen = $slen + length($STYPE) + length($1);
$ilen = index($_, "Instance Name");
# XXX add ilen checking in case more columns are added in future after it
}
}
else
{
my $service = substr($_, $slen, $nlen - $slen);
my $instance = substr($_, $ilen);
$service =~ s/^\s+//;
$service =~ s/\s+$//;
$instance =~ s/^\s+//;
$instance =~ s/\s+$//;
# if this is actual service we want more info on
if ($service =~ /local\.$/)
{
$services{$instance} = $service
}
else
{
$lookup{$instance} = $service;
}
}
}, $t);
}
sub lookup
{
my ($s, $i, $t) = @_;
$t ||= 1;
my $cmd = "$DNSSD -L '$s' $i $DOMAIN";
runt($cmd, sub {
print STDERR $_ if $DEBUG;
# /can be reached at (\S+):(\S+) \(interface (\d+)/
if (/can be reached at (\S+)/)
{
print "$1:$s:$i\n" unless $hosts{$1}++;
}
}, $t);
}
sub runt
{
my ($cmd, $cb, $timeout) = @_;
$timeout ||= 3;
eval
{
my $pid = 0;
local $SIG{ALRM} = sub { kill(9, $pid) if $pid; die };
alarm($timeout);
print STDERR "running $cmd (alarm $timeout)\n" if $DEBUG;
$pid = open(my $d, "$cmd|") || die "failed to run $cmd: $!";
while (<$d>)
{
&{$cb}($_);
}
#close(D);
alarm(0);
};
}