How to exclude some text in echo

How can I shorten output of this command.

nslookup myip.opendns.com resolver1.opendns.com > %~d0\bat\setup\ipaut\myip.txt

This gives me a text file:

Server:  resolver1.opendns.com
Address:  208.67.222.222

Name:    myip.opendns.com
Address:  84.48.190.45

What I actually want in the text file is the myip.opendns.com address. Example

84.48.190.45

Upvotes: 0

Views: 531

Answers (2)

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

set "var="
for /F "tokens=2 delims=: " %%a in ('nslookup myip.opendns.com resolver1.opendns.com') do (
   if not defined var (
      set "var=%%a"
   ) else (
      set "!var!=%%a"
      set "var="
   )
)

echo %myip.opendns.com%> output.txt

Upvotes: 2

Squashman
Squashman

Reputation: 14290

FOR /F "tokens=2 delims=: " %%G in ('nslookup myip.opendns.com resolver1.opendns.com') do >myip.txt echo %%G

This parses the output of the nslookup command and outputs the result to a text file.

Upvotes: 1

Related Questions