Page 1 of 1
Use Netsh to save ip gw dns to variables for project
Posted: 11 Jan 2025 17:27
by crobertson
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
Re: Use Netsh to save ip gw dns to variables for project
Posted: 11 Jan 2025 22:08
by Aacini
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
Re: Use Netsh to save ip gw dns to variables for project
Posted: 15 Jan 2025 08:27
by crobertson
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
Re: Use Netsh to save ip gw dns to variables for project
Posted: 15 Jan 2025 12:30
by crobertson
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!
Re: Use Netsh to save ip gw dns to variables for project
Posted: 17 Jan 2025 15:45
by crobertson
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!
Re: Use Netsh to save ip gw dns to variables for project
Posted: 18 Jan 2025 18:09
by crobertson
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%
Re: Use Netsh to save ip gw dns to variables for project
Posted: 20 Jan 2025 02:55
by miskox
Please use code tags.
See
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