Skip to content

Commit a61cbb6

Browse files
committed
Add bin/check-dns.php script
1 parent 06fd2a4 commit a61cbb6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

bin/check-dns.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env php
2+
<?php /* vim: set colorcolumn=: */
3+
/**
4+
* @project bnetdocs-web <https://github.com/BNETDocs/bnetdocs-web/>
5+
*
6+
* Dec 5 2020 - Resolve DNS entries from JSON input to A/AAAA/CNAME records, intended to be piped from bin/discover-dns.php.
7+
*/
8+
declare(strict_types=1);
9+
10+
$user_input = file_get_contents('php://stdin');
11+
$json = json_decode($user_input);
12+
13+
if (json_last_error() !== 0)
14+
{
15+
die('json error, check input');
16+
}
17+
18+
$scan = $json;
19+
while ($scan)
20+
{
21+
$scan = do_scan($scan);
22+
echo PHP_EOL;
23+
}
24+
25+
function do_scan($names)
26+
{
27+
$late_scan = [];
28+
29+
foreach ($names as $name)
30+
{
31+
$record_a = dns_get_record($name, DNS_A);
32+
$record_aaaa = dns_get_record($name, DNS_AAAA);
33+
$record_cname = dns_get_record($name, DNS_CNAME);
34+
$records = array_merge($record_a ?? [], $record_aaaa ?? [], $record_cname ?? []);
35+
36+
if (count($records) == 0)
37+
{
38+
printf("%s: %s", $name, PHP_EOL);
39+
}
40+
else
41+
{
42+
$ips = [];
43+
foreach ($records as $record)
44+
{
45+
if (isset($record['ip']))
46+
{
47+
$ips[] = $record['ip'];
48+
}
49+
else
50+
{
51+
$ips = ['-> ' . $record['target']];
52+
if (in_array($record['target'], $names))
53+
{
54+
$late_scan[] = $record['target']; // add the alias
55+
}
56+
}
57+
}
58+
sort($ips);
59+
printf("%s: %s%s", $name, implode(' ', $ips), PHP_EOL);
60+
}
61+
}
62+
63+
return $late_scan;
64+
}

0 commit comments

Comments
 (0)