E
E
EnumaElis2019-12-05 14:00:25
Command line
EnumaElis, 2019-12-05 14:00:25

Script for changing network settings, why doesn't it work "on the fly"?

Initially, the script was taken from somewhere on the net, modified a little for myself (sorry for the crooked English):

@ECHO OFF

:: Variables (set IFACE name as in your adapter settings name)
SET IFACE="Ethernet"
SET IP=192.168.0.10
SET IPD=DHCP
SET MASK=255.255.255.0
SET GATEWAY=192.168.0.1
SET GWMETRIC=1
SET DNS1=8.8.8.8
SET DNS2=77.88.8.8

:: Script
SET /P settings="Choose option (1 - STATIC, 2 - DHCP): "
ECHO Current option - %settings%

IF %settings% EQU 1 (GOTO STATIC) ELSE (IF %settings% EQU 2 (GOTO DHCP) ELSE (GOTO WRONG))

:WRONG
ECHO WRONG OPTION!
GOTO QUIT

:STATIC
ECHO Current choosen network settings:
ECHO IP-address: %IP%
ECHO Network mask: %MASK%
ECHO Gateway: %GATEWAY%
netsh interface ip set address %IFACE% static %IP% %MASK% %GATEWAY% %GWMETRIC%
ECHO Primary DNS server: %DNS1%
netsh interface ip set dns %IFACE% static %DNS1% validate=no
ECHO Secondary DNS server: %DNS2%
netsh interface ip add dns %IFACE% %DNS2% index=2 validate=no
GOTO IPSHOWQ

:DHCP
ECHO Current choosen network settings: === %IPD% ===
netsh interface ip set address %IFACE% %IPD%
netsh interface ip set dns %IFACE% %IPD%
GOTO IPSHOWQ

:IPSHOWQ
SET /P IPINFO="Show network configuration? (y - YES, n - NO): "
IF %IPINFO% NEQ y GOTO QUIT

:IPSHOW
::ipconfig /all
netsh interface ip show addresses name=%IFACE%
netsh interface ip show dns name=%IFACE%

:QUIT
ECHO Bye-bye!
pause

And it seems to work, but with quirks: the settings change only when the cable is physically connected, which is not quite what is needed in this case. Without a cable, the script works as it should, but the settings remain the same (judging by the output of ipconfig / all or through the "Details" interface in the "Adapter Properties").
What is wrong and how to win so that the settings change without a connected vituhi?
UPD: The output of these commands in the console:
netsh interface ip show addresses name=%IFACE%
netsh interface ip show dns name=%IFACE%

Always shows changes in DNS server settings, but IP settings remain the same.
PS The metric in this case, as I understand it, did not make sense to specify at all, but I left it as it was in the original script.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question