Skip to main content
updated batch file with my edits, described in upcoming comment
Source Link
@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%

timeout /t 5
ipconfig/all

@echo off
set interface="Ethernet 2"
set extra_ip=10.0.0.33
set extra_mask=255.255.248.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Waiting for IP to stabilize...
timeout /t 5

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0" ^| find /v "::" ') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway...
echo (IP %IP%, mask %MASK%, gw %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)...
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding secondary IP...
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%

timeout /t 5
echo.
echo New IP configuration:
ipconfig /all

del %temp\ipconfig.txt

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)

@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%

timeout /t 5
ipconfig/all

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)


@echo off
set interface="Ethernet 2"
set extra_ip=10.0.0.33
set extra_mask=255.255.248.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Waiting for IP to stabilize...
timeout /t 5

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0" ^| find /v "::" ') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway...
echo (IP %IP%, mask %MASK%, gw %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)...
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding secondary IP...
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%

timeout /t 5
echo.
echo New IP configuration:
ipconfig /all

del %temp\ipconfig.txt

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

added 20 characters in body
Source Link
Rik
  • 13.4k
  • 1
  • 36
  • 41

I wrote a small batch-file. You can test to see if it works in your situation. (here it works fine)

  • It will set your interface back to DHCP.
  • After that it will extract IP, Subnet Mask, Default Gateway and the first DNS-server. This is the difficult bit. Especially if you have multiple interfaces. If it doesn't work we need to fiddle here a bit to get it to work.
  • If you want the second DNS too it should be added here (but i didn't look that far because one DNS should be fine for now).
  • It will set these settings "static" on the interface.
  • After that you can add the 10.x.y.z address to your interface without a problem.

Here is the script:

@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask% 

timeout /t 5
ipconfig/all

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)

I wrote a small batch-file. You can test to see if it works in your situation. (here it works fine)

  • It will set your interface back to DHCP.
  • After that it will extract IP, Subnet Mask, Default Gateway and the first DNS-server. This is the difficult bit. Especially if you have multiple interfaces. If it doesn't work we need to fiddle here a bit to get it to work.
  • If you want the second DNS too it should be added here (but i didn't look that far because one DNS should be fine for now).
  • It will set these settings "static" on the interface.
  • After that you can add the 10.x.y.z address to your interface without a problem.

Here is the script:

@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%
ipconfig/all

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)

I wrote a small batch-file. You can test to see if it works in your situation. (here it works fine)

  • It will set your interface back to DHCP.
  • After that it will extract IP, Subnet Mask, Default Gateway and the first DNS-server. This is the difficult bit. Especially if you have multiple interfaces. If it doesn't work we need to fiddle here a bit to get it to work.
  • If you want the second DNS too it should be added here (but i didn't look that far because one DNS should be fine for now).
  • It will set these settings "static" on the interface.
  • After that you can add the 10.x.y.z address to your interface without a problem.

Here is the script:

@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask% 

timeout /t 5
ipconfig/all

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)

Source Link
Rik
  • 13.4k
  • 1
  • 36
  • 41

I wrote a small batch-file. You can test to see if it works in your situation. (here it works fine)

  • It will set your interface back to DHCP.
  • After that it will extract IP, Subnet Mask, Default Gateway and the first DNS-server. This is the difficult bit. Especially if you have multiple interfaces. If it doesn't work we need to fiddle here a bit to get it to work.
  • If you want the second DNS too it should be added here (but i didn't look that far because one DNS should be fine for now).
  • It will set these settings "static" on the interface.
  • After that you can add the 10.x.y.z address to your interface without a problem.

Here is the script:

@echo off
set interface="Local Area Connection"
set extra_ip=10.0.0.33
set extra_mask=255.255.255.0

echo Setting %interface% back to DHCP
netsh int ipv4 set address name=%interface% source=dhcp
netsh int ipv4 set dnsservers name=%interface% source=dhcp

echo Getting current IP of %interface%
ipconfig > %temp%\ipconfig.txt
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "IPv4" ^| find /v "127.0"') do set _IP=%%a
set IP=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Subnet" ^| find /v "127.0"') do set _IP=%%a
set MASK=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "Default" ^| find /v "127.0"') do set _IP=%%a
set GATE=%_IP:~1%
for /f "tokens=2 delims=:" %%a in ('type %temp%\ipconfig.txt ^| find "DNS Servers" ^| find /v "127.0"') do set _IP=%%a
set DNS1=%_IP:~1%

echo Setting IP Address, Subnet Mask and Default Gateway
echo (%IP% %MASK% %GATE%)
netsh int ipv4 set address name=%interface% static %IP% %MASK% %GATE% gwmetric=1

timeout /t 5
echo Setting Primary DNS (%DNS1%)
netsh int ipv4 set dnsserver name=%interface% static %DNS1% primary

echo Adding 10.x.y.z
netsh int ipv4 add address %interface% %extra_ip% %extra_mask%
ipconfig/all

You only need to run this once a period of your lease (or after a restart). So if your lease is 10 days you could set this in the task scheduler for 3 AM on Sunday and after every restart. If your computer is always off at night it would only be needed after restart.

Edit: I had to add a timeout /t 5 before setting the DNS. (otherwise i got an error)