cmd - Batch File get Specific IP into Variable -
i trying select specific ip address in windows batchfile. line of code works, gets wrong ip address:
for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr /r /c:"ipv4 address"') (set newip=%%a)
if type @ command line, gets single line containing ip address i'm looking for:
netsh interface ipv4 show addresses "ethernet 2" | findstr /r /c:"ip address"
however, when try embed line above ip address variable, throws error "'else' not recognized internal or external command, operable program or batch file.'
for /f "delims=: tokens=2" %%a in ('netsh interface ipv4 show addresses "ethernet 2" ^| findstr /r /c:"ip address"') (set newip=%%a)
any thoughts i'm doing wrong?
fyi, reason why i'm trying select have program sets entry in hosts file point current ip, site i'm running locally can use specific domain. problem existing code have 2 ipv4 addresses - 1 vpn, , other local router - , code selecting wrong ip address.
many thanks!
your findstr parameters wrong. have specified use regular expression , use literal. regex win searching ip or address. remove /r
.
also tokens/delims wrong - getting lot of spaces in variable. makes colon , space delimiter.
for /f "tokens=3 delims=: " %a in ('netsh interface ipv4 show addresses "local area connection 7" ^| findstr /c:"ip address"') (set newip=%a)
how did discover this. unwound command individual commands , ran each of those. once see output obvious.
Comments
Post a Comment