NickM
NickM

Reputation: 1

Single Line FOR /F command FINDSTR error handling unexpected results

Problem defined is to have a command line that takes the output of nslookup from a FOR /F loop and either sends to the IP Address to a text file or sends a error message.

The simplest iteration I have is:

FOR /F "skip=3 usebackq tokens=1*" %a IN (`^"nslookup ADROOT02.adroot.the-server-company.co.za 2^>^&1^"`) DO @echo %a | findstr /C:"Address" >nul 2>&1 && echo found || echo not found

However, this only works if an address is found, but no output is received if the findstr fails; i.e. no || echo not found is not activated for some reason.

I am looking for assistance is finding the issue/resolution to have a single command line executable that can define the IP addresses.

Upvotes: 0

Views: 195

Answers (2)

NickM
NickM

Reputation: 1

With guidance from this forum I was able to put a command together that outputs the IP address from nslookup or error if no IP found:

FOR /F "usebackq tokens=1* delims=: " %a IN (`^"nslookup server-company.co.za 2^>^&1` ^| findstr ^/N ^/C^:^"Address^" 2^>^&1 ^"`) DO IF %a GTR 3 ( SET res2=%b & echo %res2:~10%) ELSE (echo ERROR)

Upvotes: 0

Magoo
Magoo

Reputation: 80033

@ECHO OFF
SETLOCAL

FOR %%d IN (google.com ADROOT02.adroot.the-server-company.co.za ) DO (
 NSLOOKUP %%d 2>&1|FIND "Non-existent domain" >nul&IF ERRORLEVEL 1 (ECHO %%d found) ELSE (ECHO %%d NOT found)
)

GOTO :EOF

Yields

google.com found
ADROOT02.adroot.the-server-company.co.za NOT found

for me.

Upvotes: 1

Related Questions