Changing IP addresses with a bat file

Solved
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 answer

  1. 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
    1. 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
    2. 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
    3. @kaumuneThank you for your quick response
      I had put together a command like that
      But could you, if you don't mind, take the formula above and insert all of that inside because I am unable to do it
      Thank you in advance
      0
    4. I'm just bumping this because I need this
      Thanks in advance
      0
    5. You must of course enter your own values in the beginning section (in italics)

      @echo off
      set card="LAN"
      set fixedaddr=192.168.0.14
      set mask=255.255.255.0
      set gateway=192.168.0.1
      set dns1=192.168.0.1
      set dns2=192.168.0.3


      :question
      SET /P lan=IP Addressing 1/DHCP 2/FIXED 3/QUIT (1/2/3)? :
      if %lan%==1 goto IPDHCP
      if %lan%==2 goto IPfixed
      if %lan%==3 goto End
      goto question

      :IPfixed
      SET /P lan=confirm the fixed IP addressing 192.168.0.14 (Y/N)? :
      if %lan%==y goto OKFixed
      if %lan%==Y goto OKFixed
      if %lan%==n goto End
      if %lan%==N goto End
      goto IPfixed

      :OKFixed
      netsh interface ip set address %card% static %fixedaddr% %mask% %gateway% 1
      netsh inter ip delete dns LAN all
      netsh inter ip set dns LAN static %dns1% primary
      netsh inter ip add dns LAN %dns2% index=2

      goto EndOf

      :IPDHCP
      SET /P lan=confirm the dynamic IP addressing (Y/N)? :
      if %lan%==y goto OKDHCP
      if %lan%==Y goto OKDHCP
      if %lan%==n goto End
      if %lan%==N goto End
      goto IPDHCP

      :OKDHCP
      netsh interface ip set address %card% dhcp
      netsh inter ip delete dns LAN all
      netsh inter ip set dns LAN dhcp
      goto EndOf

      :End
      @echo No modifications have been applied
      @echo -
      SET /P lan=press [ENTER] to quit
      goto fin

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

      :fin
      0