连续监听Java的DNS请求,执行cmd命令

Listen DNS requests with Java in continuous and execute cmd command

好的,让我们说得更准确些。许多天来我一直在问自己一个问题:是否可以在计算机上监听 DNS 请求(在我的例子中是 windows 机器)并要求程序在检测到特殊 DNS 时执行 shell 命令 ?

我一直在玩这个命令:

ipconfig /displaydns |找到 "my.dns.com"

正如预期的那样,我能够在我的 DNS 缓存中找到 "my.dns.com"。

但是 我无法向命令询问:

Hey, listen for all the requests in continuous and when you detect "my.dns.com" please execute "music.exe".

所以我设想了一个程序(在 Java 中)为我做这件事。为什么 java ?因为它不是我能够使用的唯一编程语言,所以我从它开始,这样我更容易理解它。但是,如果我要找的东西在 Java 中是不可能的,请告诉我!

但是(再次)我不知道如何管理 DNS 请求,也没有要求 java 程序连续 运行。目前,这里是我可以添加到程序中的所有内容:

if ("my.dns.com" is detected) {
try {
String command = "cmd /c start music.exe";
Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
} catch (IOException e) {
}

也许 运行 连续 :

boolean running = true;
while(running)
{
  //main loop...
}

我知道,这很少,但我什至不知道去哪里寻找答案。

拜托,如果你能理解我的话(是的,你在屏幕后面吃着多力多滋)能不能至少帮我找到路?

如果你想让我编辑它,请精确:)

也许 PowerShell 的这个(未经测试的)剪辑可以帮助您前进。我不知道清除 DNS 缓存是否是个好主意,但这取决于您。如果你不这样做,它仍然会在那里。

while ($true) {
    $a = Get-DnsClientCache | ForEach-Object { $_.Name }
    if ($a -contains 'the.dns.com') {
        Start-Process C:\path\to\music.exe -Wait
        Clear-DnsClientCache
    }
}