0

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?

shebelaw
  • 3,992
  • 6
  • 35
  • 48

4 Answers4

0

I found this on DOS Tips

BatchSubstitute - parses a File line by line and replaces a substring

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitute - parses a File line by line and replaces a substring
::syntax: BatchSubstitute.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
::$changed 20100115
::$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
unclemeat
  • 5,029
  • 5
  • 28
  • 52
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • I want to replace multi word string with special characters present in that like my string is 'node "%~dp0\..\mocha\bin\mocha" %*'. I need to change that to 'my_path "%~dp0\..\mocha\bin\mocha" %*' So how can i do that using above script. – Eswara Reddy Jul 11 '17 at 11:06
  • @EswaraReddy You should put up a new question with all the details of what you tried (referencing this one if you tried it) and all the results. – Preet Sangha Jul 13 '17 at 00:08
0

If you want to replace just one line, you can do it this way:

@echo off
setlocal

cd "C:\WINDOWS\system32\drivers\etc"
for /F "delims=:" %%a in ('findstr /N "myAppServer" hosts') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" hosts') do (
   if %%a equ %lineNum% (
      echo 192.168.11.9 www.myAppServer.com
   ) else (
      echo %%a
   )
)) > hosts.new

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

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

set "hostsFile=%systemroot%\system32\drivers\etc\hosts"
(
    findstr /v /i /c:"www.MyAppServer.com" "%hostsFile%"
    echo(
    echo %IP% www.MyAppServer.com
) > "%temp%\hosts"

move /y "%temp%\hosts" "%hostsFile%" >nul 2>nul

And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • right that is what I finally end up dong, as shown on my answer at the bottom. Will there be a way for me to do the same thing on the host OS, [Windows, OS X, Linux, ..] on mac `ifconfig` won't show me the ip of the virtual boxes. – shebelaw Dec 03 '13 at 16:31
0

this get the job done finally:

@echo off
REM Set a variable for the Windows hosts file location
set "hostpath=%systemroot%\system32\drivers\etc"
REM set "hostpath=C:\projectStar\Programs\etc" 
set "hostfile=hosts"

REM Make the hosts file writable
attrib -r -s -h "%hostpath%\%hostfile%"

for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
REM echo %IP%
set IP=%IP%

move /y "%hostpath%\%hostfile%" "Hs.txt"
findstr /v/c:"www.nexsoftnetwork.com" Hs.txt > "Hss2.txt"
echo %IP%   www.nexsoftnetwork.com >>"Hss2.txt"

move /y "Hss2.txt" "%hostpath%\%hostfile%"

echo Done.
pause

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.

shebelaw
  • 3,992
  • 6
  • 35
  • 48