I have a local server on my Windows XP Virtual Box.
Every time my main network changes like from wifi to cable or I move to a different network the IP of my Win XP box changes.
I want to run a batch file to map the new IP to some common name in my hosts file
C:\WINDOWS\system32\drivers\etc\hosts
for example if I have now:
10.97.100.74 www.myAppServer.com
and if the ip changes to 192.168.11.9
,
I want to search for the string myAppServer
in the hosts file and replace the whole line with
192.168.11.9 www.myAppServer.com
I was able to get the current IP using:
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
set IP=%IP%
but how do I find that line and replace it?
The simplest way of doing it is not replacing it. Remove it and add a new line.
If you have your ip address in %IP%, then
And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.
If you want to replace just one line, you can do it this way:
This program eliminate empty lines and will have problems if the hosts file contain Batch special characters, like
| > < & !
. These details may be fixed, if needed.I found this on DOS Tips
BatchSubstitute - parses a File line by line and replaces a substring
this get the job done finally:
thank you all for your inputs. But I wanted to hit this server from my phone which will be on the same network as the server. and eventually I came to notice that unless I modify the hosts file of the host operating system or the mobile phone it won't still solve my problem. this solution works only if I want to hit this server with in the virtual box.