# Solved: Update list of IP addresses (txt file) using nslookup



## Zinola (Jan 27, 2010)

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 (Apr 4, 2003)

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.

```
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.


```
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 (Jan 27, 2010)

I still have not resolved the problem.


----------



## Squashman (Apr 4, 2003)

That doesn't tell us anything! Could you be more specific.


----------



## Squashman (Apr 4, 2003)

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 (Jan 27, 2010)

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 (Apr 4, 2003)

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.

```
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.


```
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.


----------



## TheOutcaste (Aug 8, 2007)

Squashman said:


> 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:

```
@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 (Jan 27, 2010)

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 (Aug 8, 2007)

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

```
For /F "Tokens=2 Delims=: " %%A In ('nslookup %%J 8.8.8.8 2^>Nul^|Findstr "Address"') Do Set _IP=%%A
```
To This:

```
For /F "Tokens=2 Delims=[B][COLOR=Red],[/COLOR][/B]: " %%A In ('nslookup %%J 8.8.8.8 2^>Nul^|Findstr "Address"') Do Set _IP=%%A
```


----------



## Squashman (Apr 4, 2003)

Zinola said:


> 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 (Jan 27, 2010)

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 (Apr 4, 2003)

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-development/907057-need-bat-file.html


----------



## TheOutcaste (Aug 8, 2007)

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.


----------

