Changing IP addresses with a bat file

Solved
tast -  
Evalon Posted messages 64 Registration date   Status Membre Last intervention   -
Hello,
I often have to change addresses, switching from a fixed address to a DHCP server
so I've created a batch file like this:

netsh interface ip set address "Local Area Connection" static 223.254.254.1 255.255.240.0

but when I run it my addresses don't change, while the command works well in DOS
Configuration: Windows XP Firefox 2.0.0.14

1 réponse

héhé
 
The problem comes from the name of your local network connection, rename it to "lan" and it will work
ex:
@echo off
set card="LAN"
set fixedaddr=192.168.0.14
set mask=255.255.255.0

:question
SET /P lan=IP Address 1/DHCP 2/FIXED 3/EXIT (1/2/3)? :
if %lan%==1 goto IPDHCP
if %lan%==2 goto IPfixed
if %lan%==3 goto Nfin
goto question

:IPfixed
SET /P lan=confirm the fixed IP address 192.168.0.14 (Y/N)? :
if %lan%==o goto OKFixed
if %lan%==O goto OKFixed
if %lan%==n goto Nfin
if %lan%==N goto Nfin
goto IPfixed

:OKFixed
netsh interface ip set address %card% static %fixedaddr% %mask%
goto Ofin

:IPDHCP
SET /P lan=confirm the dynamic IP address (Y/N)? :
if %lan%==o goto OKDHCP
if %lan%==O goto OKDHCP
if %lan%==n goto Nfin
if %lan%==N goto Nfin
goto IPDHCP

:OKDHCP
netsh interface ip set address %card% dhcp
goto Ofin

:Nfin
@echo No changes have been applied
@echo -
SET /P lan=press [ENTER] to exit
goto fin

:Ofin
@echo The new configuration has just been applied
@echo -
SET /P lan=press [ENTER] to exit
goto fin

:fin

additionally, you can choose fixed or dhcp
so rename your connection to "lan" in your network connections and in your bat file.
5
Evalon Posted messages 64 Registration date   Status Membre Last intervention   3
 
Hello,

I know this topic is old but I found this little script really nice.
But how can I add the default gateway and the dns.
Thanks in advance.
0
kaumune Posted messages 22600 Registration date   Status Contributeur Last intervention   5 156
 
Assuming you have renamed your network to LAN

For an IP of 192.168.0.1 and a gateway of 192.168.0.2 it gives

netsh inter ipv4 set address LAN static 192.168.0.1 255.255.255.0 192.168.0.2

for the DNS as an example 192.168.0.1 as primary

netsh inter ipv4 delete dns LAN all

netsh inter ipv4 set dns LAN static 192.168.0.1 primary


possibly a secondary DNS 192.168.0.3

netsh inter ipv4 add dns LAN 192.168.0.3 index=2
1