Solved

Batch File Help - Set variable from NSLOOKUP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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

22 Experts available now in Live!

Get 1:1 Help Now