Solved

Batch File Help - Set variable from NSLOOKUP

Posted on 2013-12-18
5
1,960 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 14

Expert Comment

by:Giovanni Heward
Comment Utility
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
Comment Utility
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 14

Expert Comment

by:Giovanni Heward
Comment Utility
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
Comment Utility
:)  Nice job.
0
 
LVL 14

Accepted Solution

by:
Giovanni Heward earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This is about my first experience with programming Arduino.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now