Solved

Batch File Help - Set variable from NSLOOKUP

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

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 14

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 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