3

I'm using nslookup and findstr in a simple batch script to check if a website is up.
However findstr keeps printing lines which doesn't contain any matches. The command goes like this:

nslookup goofdedwsgle.com  | findstr /X /C:"Non-existent domain"

which results in

*** one.one.one.one can't find goofdedwsgle.com: Non-existent domain

Which is what I'm looking for. However if I use an existent domain:

nslookup google.com 1.1.1.1 | findstr /X /C:"Non-existent domain"

I get this which is definitely not a match:

Non-authoritative answer:

It seems this is from the full nslookup output:

Server:  one.one.one.one
Address:  1.1.1.1

Non-authoritative answer: Name: google.com Addresses: 2404:6800:4004:80a::200e 142.250.66.78

but why does findstr print out that line? /X should guarantee findstr only print out matches right?
Even weirder, if I make a txt file with that same content as the nslookup result, then run findstr /X /C:"Non-existent domain" on that, it shows up empty.
What is the issue here?

Mizuki
  • 33

2 Answers2

1

nslookup seems to always output Non-authoritative answer: in stderr stream which is the issue here. Even though that case is common.

You can redirect stderr to nul (or &1 = stdin if needed) as a workaround:

nslookup google.com 1.1.1.1 2>nul | findstr /X /C:"Name:    google.com"

Result:

Name: google.com

Or use more modern PowerShell functions which don't have this issue and filtering on them is also easier:

Resolve-DnsName -Name google.com -Server 1.1.1.1
Destroy666
  • 12,350
0

You can also redirect stderr "2" to >& the stdout "1" and filter with a simpler find "string" command, or findstr /end "string", or even with with a simple findstr "string"

Redirect stderr to stdout 2&1:

nslookup google.com 1.1.1.1 2>&1 | findstr /e "google.com"
Nome:    google.com

nslookup google.com 1.1.1.1 2>&1 | findstr "google.com" Nome: google.com

nslookup google.com 1.1.1.1 2>&1 | find "google.com" Nome: google.com


Obs.1: As much as the output is/returns Name:[some spaces]google.com, you can also consider that what you see in this "vacant" space are spaces, but it can be a TAB, and rarely some non-printable character. so that would explain some behavior when using /x, even though stderr messages appear to you.

Name:TABgoogle.com
Name:SpaceSpaceSpaceSpacegoogle.com
Name: One or More Nonprinting Characters google.com

Obs.2: I'm not saying that nsloopkup generates any of these characters, I'm telling you to observe when using /x in the output treatment that because they don't "print" something visible, any occurrence of these potential characters can interfere with findstr's behavior when filtering specifically the /x same character and/or /x same_characters_strings.


Additional resources:

Io-oI
  • 9,237