Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop dns driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: Update list of IP addresses (txt file) using nslookup

Reply  
Thread Tools
Zinola's Avatar
Junior Member with 5 posts.
 
Join Date: Jan 2010
Experience: Intermediate
29-Jan-2010, 12:24 PM #1
Solved: Update list of IP addresses (txt file) using nslookup
Hello,
Someone can help me please to solve the following problem:
My main objective is to update a list of IP addresses that I have written to a text file (IPlist.txt).
To get the address of a particular website (for example forums.techguy.org), I use the following code:

----------------------------------------------------------------------
@ECHO OFF
nslookup forums.techguy.org 8.8.8.8|findstr "Address:" > tmp.txt
FOR /F "tokens=2 skip=1 delims=: " %%d in (tmp.txt) do SET targetip=%%d
----------------------------------------------------------------------


The result will be: targetip = 209.183.226.152

The text file (IPlist.txt) is written as follows:
___________________________________________________
# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
___________________________________________________

and is saved in: "C:\Users\TOSHIBA\IPlist.txt"

Therefore, I intend to get the IP address of a website (for example forums.techguy.org) through the code above and then replace the old IP address (209.183.226.152) that is in the text file (IPlist.txt) for the new IP address found by nslookup (for example let's assume that nslookup gave the new IP address 198.199.200.201).
So when you run the script, the text file (IPlist.txt) would be with the following contents:
___________________________________________________
# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
198.199.200.201 forums.techguy.org
209.251.180.18 usa.gov
___________________________________________________



In summary, the main objective is to update the IP addresses in a text file ("C:\Users\TOSHIBA\IPlist.txt") stored on my computer's hard drive every time I run nslookup.

Someone can help me to create the code in a batch file?

Thank you in advance, any help is welcome.

Best regards
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
29-Jan-2010, 03:03 PM #2
You can't in place edit a text file with pure batch.

You will have output to a new text file.

Just for future reference you can execute code in the for loop.
Code:
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup forums.techguy.org 8.8.8.8^|findstr "Address:"') do SET targetip=%%d
You would have to add a nested for loop inside the first for loop to parse the IPlist.txt and then output it to a newfile.

Code:
set _Domain=forums.techguy.org
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup %_Domain% 8.8.8.8^|findstr "Address:"') do (
     SET targetip=%%d
     FOR /F "tokens=1* delims= " %%I in (IPlist.txt) do (
     IF /I %_Domain% == %%J (
          echo %%d %%J>>IPListNew.txt
     ) else (
          echo %%I %%J>>IPListNew.txt
     )))
I think that will work. This is just off of the top of my head.
Zinola's Avatar
Junior Member with 5 posts.
 
Join Date: Jan 2010
Experience: Intermediate
04-Feb-2010, 12:58 PM #3
I still have not resolved the problem.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
04-Feb-2010, 01:29 PM #4
That doesn't tell us anything! Could you be more specific.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
04-Feb-2010, 07:17 PM #5
Well just for the sake of argument I retested the batch file even though TheOutcaste told it worked fine for him as well. It also worked fine for me. You will have to provide use more detail as to what your problem is.
Zinola's Avatar
Junior Member with 5 posts.
 
Join Date: Jan 2010
Experience: Intermediate
05-Feb-2010, 08:23 AM #6
I'm sorry, I made a beginner's fault.
The solution is on track.
However when I run the code above three times (for example), the text file IPlistNew.txt is written:

# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov

Now my question is:
Is there no possibility to delete the first lines of text file IPlistNew.txt?
So every time I run the code (10, 300, n times), the text file IPlistNew.txt should always be written:


# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
05-Feb-2010, 10:44 AM #7
The two ">>" means you are appending to the file.
The better way to do this would be to rename the files back to the original before you run it again.

So at the end of the batch file just delete the original IPlist.txt and rename IPlistNew.txt to IPlist.txt.
Code:
set _Domain=forums.techguy.org
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup %_Domain% 8.8.8.8^|findstr "Address:"') do (
     SET targetip=%%d
     FOR /F "tokens=1* delims= " %%I in (IPlist.txt) do (
     IF /I %_Domain% == %%J (
          echo %%d %%J>>IPListNew.txt
     ) else (
          echo %%I %%J>>IPListNew.txt
     )))
del IPlist.txt
rename IPlistNew.txt IPlist.txt
Instead of running this code three times, you can just put all the domains you want to lookup into a text file and read the text file into a loop.. You would basically nest another For Loop outside of your existing code. If you wanted to update all the domains in IPlist.txt you could use that as your input instead of manually defining each domain you want to lookup.

Code:
FOR /F "tokens=1* delims= " %%A in (IPlist.txt) do (
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup %%B 8.8.8.8^|findstr "Address:"') do (
     FOR /F "tokens=1* delims= " %%I in (IPlist.txt) do (
     IF /I %%B == %%J (
          echo %%d %%J>>IPListNew.txt
     ) else (
          echo %%I %%J>>IPListNew.txt
     ))))
del IPlist.txt
rename IPlistNew.txt IPlist.txt
Pretty sure I can shorten up the code some more if you go with this second solution. I really shouldn't have to nest an additional For Loop now that I think about it.

Last edited by Squashman; 05-Feb-2010 at 10:51 AM..
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
05-Feb-2010, 10:55 AM #8
Quote:
Originally Posted by Squashman View Post
If you wanted to update all the domains in IPlist.txt you could use that as your input instead of manually defining each domain you want to lookup.
Something like this will work. This version preserves the current IP address in the file if the nslookup fails for some reason, and works if the nslookup returns multiple addresses:
Code:
@Echo Off
SetLocal EnableDelayedExpansion
Set _SFile=IPlist.txt
Set _DFile=IPlistNew.txt
If Exist "%_DFile%" Del "%_DFile%"
For /F "Usebackq Tokens=1* Delims= " %%I In ("%_SFile%") Do If %%I==# (
  >>"%_DFile%" Echo.%%I %%J
) Else (
  For /F "Tokens=2 Delims=: " %%A In ('nslookup %%J 8.8.8.8 2^>Nul^|Findstr "Address"') Do Set _IP=%%A
  If !_IP!==8.8.8.8 (
    Echo.%%I %%J>>"%_DFile%"
  ) Else (
    Echo.!_IP! %%J>>"%_DFile%"
  ))  
>Nul Move /Y "%_DFile%" "%_SFile%"
Zinola's Avatar
Junior Member with 5 posts.
 
Join Date: Jan 2010
Experience: Intermediate
10-Feb-2010, 01:44 PM #9
You are experts in the field.
Thank you for your cooperation.
However there is a detail of relative importance that has a flaw:
In the case of google.com that returns multiple addresses (Addresses: 209.85.227.104, 209.85.227.147, 209.85.227.105, 209.85.227.103, 209.85.227.99, 209.85.227.106), the result after running the code from previous post is:

# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.227.104, google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov

Comma appears after the IP address.
Apart from that, the update is done with great success.
Again thank you.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Feb-2010, 02:33 PM #10
My bad for not testing on XP. Win7 lists the addresses on separate lines.

Change line 10 to add a comma as a delimiter, that should fix it.
From this
Code:
For /F "Tokens=2 Delims=: " %%A In ('nslookup %%J 8.8.8.8 2^>Nul^|Findstr "Address"') Do Set _IP=%%A
To This:
Code:
For /F "Tokens=2 Delims=,: " %%A In ('nslookup %%J 8.8.8.8 2^>Nul^|Findstr "Address"') Do Set _IP=%%A
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
10-Feb-2010, 02:55 PM #11
Quote:
Originally Posted by Zinola View Post
You are experts in the field.
Thank you for your cooperation.
However there is a detail of relative importance that has a flaw:.
Not a flaw. It is a feature!
Zinola's Avatar
Junior Member with 5 posts.
 
Join Date: Jan 2010
Experience: Intermediate
02-Mar-2010, 01:18 PM #12
Hello,
There is no doubt that the code works fine however lately I have been a little problem.
The code is installed on a computer that is used by several users, and there are some users who usually run the code twice.
This creates a problem because they do not let the code terminates the execution.
I will present an example:
Let's assume that the IP address list is as follows:
___________________________________________________
# List of IP addresses stored in the text file:
207.68.172.246 msn.com
209.85.229.147 google.com
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
___________________________________________________


When you run the code once and it is expected that the execution is finished, everything works properly and all the IP addresses on the list are updated.
However there are some users who run the code again and again and not let the first run is completed, ie, there are some IP addresses in the list that are lost. And the list of IP addresses is reduced to (for example):
___________________________________________________
209.183.226.152 forums.techguy.org
209.251.180.18 usa.gov
___________________________________________________


So, my question is:
Is there a way to enter a code in the batch file that prevents a second run before the first has been completed?
In other words:
Skip next plays while the code of the batch file running in order to update the IP list in good condition and not lose addresses in the list.

Someone can help me on this subject please?

Regards
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
02-Mar-2010, 01:28 PM #13
yes. TheOutCaste basically just posted some code in another thread to check to see if a batch file is currently running using tasklist.

At the beginning of the batch file you could use that code to see if it is already running. If it is end the batch file.
http://forums.techguy.org/software-d...-bat-file.html
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
02-Mar-2010, 02:06 PM #14
You'd have to check to see if the file is already running before you set the title though, or it will see itself and just exit.
Another option is to use a temp file as an "I'm running flag", but if the program is stopped by CTRL+C the file doesn't get deleted.

I think the best option is to set the title then count how many processes are running using that title. If it's more than one you exit. This would handle the case where somebody double clicks twice on the file fast enough to start two copies at the same time. If both happen to check for a process with the title before either has set the title, both will run.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:04 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.