Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File Help - Set variable from NSLOOKUP

Posted on 2013-12-18
5
Medium Priority
?
2,195 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
  • 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

877 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