Solved

Batch File Help - Set variable from NSLOOKUP

Posted on 2013-12-18
5
2,037 Views
Last Modified: 2013-12-19
Hello,

I know it is possible to direct output of a command to a text file, then set specific variable information out of this..

I need a batch file that will do an "nslookup www.test.com > lookup.txt" then get the IP of the domain and set is as a variable.

I found an example online to get the gateway IP and set the IP as a variable:

echo > "%userprofile%\gate.txt"
ipconfig | find /i "gate" > "%userprofile%\gate.txt"
For /F "Tokens=2* Delims=: usebackq" %%i In ("%userprofile%\gate.txt") Do Set gate=%%i
del /f /q "%userprofile%\gate.txt"
set gate=%gate: =%
for /f "Tokens=1,2,3,4 Delims=." %%i In ("%gate%") Do Set a=%%i& Set b=%%j& Set c=%%k& Set d=%%l
set gate=%a%.%b%.%c%.%d%


I want to do the same thing as the example above but get the IP of a website via NSlookup and then set the IP as a variable. I need a script to do this since the IP of this particular website uses dynamic DNS and the IP will change frequently... so I need the script to detect the new IP via the nslookup.

If there are multiple IP addresses for the website, i just need to use the first one in the list.

Can you provide an example how to do this?
0
Comment
Question by:zequestioner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 39728121
You don't need to necessarily write it to a file, you can store it in a variable directly.

For example:

Command line:
for /f "skip=4 tokens=2" %f in ('nslookup domain.com') do echo %f

Open in new window


Batch file:
for /f "skip=4 tokens=2" %f in ('nslookup domain.com') do (
     set ip=%%f
     echo IP: %ip%
)

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 39728134
How do you handle multiples?

C:\Users\user1>for /f "skip=5" %f in ('nslookup www.google.com') do echo %f
Non-authoritative answer:

C:\Users\user1>echo 74.125.225.176
74.125.225.176

C:\Users\user1>echo 74.125.225.179
74.125.225.179

C:\Users\user1>echo 74.125.225.178
74.125.225.178

C:\Users\user1>echo 74.125.225.177
74.125.225.177

C:\Users\user1>echo 74.125.225.180
74.125.225.180

C:\Users\user1>

Open in new window

0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 39728139
If there are multiple IP addresses for the website, i just need to use the first one in the list.

This works fine for that purpose:
@echo off
setlocal enabledelayedexpansion
for /f "skip=4 tokens=2" %%f in ('nslookup google.com 2^>nul') do (
	set ip=%%f
	echo IP: !ip!
	goto :eof
)

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 39728151
:)  Nice job.
0
 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 500 total points
ID: 39728157
Thanks.  This version will be more reliable over time.  Forces use of IPv4 addresses, not as susceptible to lookup timeout error messages, etc.

@echo off
setlocal enabledelayedexpansion
set domain=example.com
ipconfig /flushdns >nul 2>&1
for /f "tokens=3" %%i in ('ping -a -n 1 -4 !domain! -w 1') do (
	set ip=%%i
	set ip=!ip:~1,-1!
	call :cont !ip!
	goto :eof
)
:cont
echo ip: %1

Open in new window


Edit:  Added step to flush local DNS cache.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question