Use Netsh to save ip gw dns to variables for project

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
crobertson
Posts: 25
Joined: 25 May 2012 12:34

Use Netsh to save ip gw dns to variables for project

#1 Post by crobertson » 11 Jan 2025 17:27

I can get these values from ipconfig, but can't sepcify the adapter I'm using.
I've got some code to get these address but not to save them as variables
I'm trying to save these address to variables to set the values so I can manually set/change IP address for several projects,
Again, I can get the information from ipconfig, but I have to turnn off wi-fi or Ethernet, where with netsh I can find the active connection.

:get
Title ===Find connection name===
FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO set conn=%%B
echo Your active connection is %conn%

:find
Title ===Find IP Gateway===
echo ===Finding IP and Gateway===
netsh interface ip show address "%conn%" | find "DHCP"
netsh interface ip show address "%conn%" | find "Address"
netsh interface ip show address "%conn%" | find "Default Gateway"
pause

:enter
Title ===Enter these values manually===
echo ===Enter these values manually===
set /p a=IP:
set /p b=GW:
set /p c=DNS:

:sets
netsh interface ip set address "Ethernet" static %a% 255.255.255.0 %b%
netsh interface ip add dns "Ethernet" %c%
netsh interface ip add dns "Ethernet" 1.1.1.1 index=2

:show
netsh interface ip show address "Ethernet"

pause

Aacini
Expert
Posts: 1921
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Use Netsh to save ip gw dns to variables for project

#2 Post by Aacini » 11 Jan 2025 22:08

Try this:

Code: Select all

@echo off
setlocal

FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO set "conn=%%B"

set "IP="
for /F "tokens=2 delims=:" %%a in ('netsh interface ip show address "%conn%" ^| findstr "Address Default"') do (
   for %%b in (%%a) do if not defined IP (set "IP=%%b") else set "Default=%%b"
)

echo Connection=%conn%
echo IP Address=%IP%
echo Default   =%Default%
Antonio

crobertson
Posts: 25
Joined: 25 May 2012 12:34

Re: Use Netsh to save ip gw dns to variables for project

#3 Post by crobertson » 15 Jan 2025 08:27

I'm try to use DNS too. I found this;
rem netsh interface ipv4 show dnsserver name="%conn%"
netsh interface ipv4 show dnsserver name="%conn%" |findstr "Servers"
for /f "tokens=3 delims=: " %%c in ('netsh interface ipv4 show dnsserver name="%conn%" ^| findstr "Servers"') do set ns=%%c
echo Your DNS is: %ns%

With no output. I'm having trouble trimming the line down to simple address

crobertson
Posts: 25
Joined: 25 May 2012 12:34

Re: Use Netsh to save ip gw dns to variables for project

#4 Post by crobertson » 15 Jan 2025 12:30

I got DNS worked out.
Title ===Find adapter name===
FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO set conn=%%B

rem Get IP, Gatway, DNS
for /f "tokens=3 delims=: " %%a in ('netsh interface ip show address "%conn%" ^| findstr "IP Address"') do set IPAddress=%%a
for /f "tokens=3 delims=: " %%b in ('netsh interface ip show address "%conn%" ^| findstr "Default"') do set GW=%%b
for /f "tokens=5 delims=: " %%c in ('netsh interface ip show dnsserver "%conn%" ^| findstr "Servers"') do set ns=%%c

echo Adapter =%conn%= show address: %IPAddress%
echo Adapter =%conn%= show gateway: %GW%
echo Adapter =%conn%= show DNS: %ns%

Thank for your help!

crobertson
Posts: 25
Joined: 25 May 2012 12:34

Re: Use Netsh to save ip gw dns to variables for project

#5 Post by crobertson » 17 Jan 2025 15:45

I got almost everything I need except for MAC address from netsh
I have been able to get mac from
arp -a | find "ipaddress" and may have to stick with that but I'm trying to use netsh
netsh -r \\ipaddress.... but I get that far and can't find commands for mac address for
netsh -r \\ipaddress inteface show interface but no bueno
netsh -r \\ipaddress interface show interface name=wlan

I"m sutck. Help!

crobertson
Posts: 25
Joined: 25 May 2012 12:34

Re: Use Netsh to save ip gw dns to variables for project

#6 Post by crobertson » 18 Jan 2025 18:09

Here is the batch snippet. I'd like to remove the - from the mac address and I'm planninig one writing to text.

rem Get IP, Gatway, DNS
for /f "tokens=3 delims=: " %%a in ('netsh interface ip show address "%conn%" ^| findstr "IP Address"') do set ip=%%a
for /f "tokens=3 delims=: " %%b in ('netsh interface ip show address "%conn%" ^| findstr "Default"') do set GW=%%b
for /f "tokens=6 delims=: " %%c in ('netsh interface ip show dnsserver "%conn%" ^| findstr "servers"') do set ns=%%c
for /f tokens^=1^-7^ delims^=^" %%1 in ('getmac /v /fo:csv /nh ') do (if "%%1"=="%conn%" (set mc=%%5))

echo Adapter =%conn%= show address: %ip%
echo Adapter =%conn%= show gateway: %GW%
echo Adapter =%conn%= show DNS: %ns%
echo Adapter =%conn%= show MAC: %mc%

miskox
Posts: 634
Joined: 28 Jun 2010 03:46

Re: Use Netsh to save ip gw dns to variables for project

#7 Post by miskox » 20 Jan 2025 02:55

Please use code tags.

See

Code: Select all

set /?
where

Code: Select all

Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.
Saso

Post Reply