Results 1 to 1 of 1

Thread: Changing the IP address by using a batch file

  1. #1
    Bronze Member carloc's Avatar
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    166

    Changing the IP address by using a batch file

    May be all of you already know how to do this... but I found how to rapidly change the IP address of my laptop just a few day ago
    I need to switch the IP because at work I need a static IP but at home I use a dynamic IP (via DHCP) so I tried to write two small .bat files that avoid me a lot of boring clicks on the network preferences dialog windows!

    setip_stat.bat
    Code:
    @rem echo OFF
    set lan_name="Local Area Connection"
    
    rem define all the network addresses here
    set subnet=192.168.1
    set lan_ip=%subnet%.22
    set sub_net_mask=255.255.255.0
    set gateway_addr=%subnet%.153
    set pri_dns_addr=%subnet%.157
    set sec_dns_addr=%subnet%.153
    
    rem set the ip address
    @netsh interface ip set address name=%lan_name% source=static addr=%lan_ip% mask=%sub_net_mask% gateway=%gateway_addr% gwmetric=1
    rem set the DNS
    @netsh interface ip set dns name=%lan_name% source=static addr=%pri_dns_addr% primary
    rem add an alternative DNS (optional)
    @netsh interface ip add dns name=%lan_name% addr=%gateway_addr% index=2
    setip_dhcp.bat
    Code:
    @rem echo OFF
    set lan_name="Local Area Connection"
    rem set the ip address
    @netsh interface ip set address name=%lan_name% source=dhcp
    rem set the DNS
    @netsh interface ip set dns name=%lan_name% source=dhcp
    Alternatively, if you are familiar with the batch file sintax, it is possible to write a single .bat file passing one ore more parameters for switching among the different parts of the code.
    I attached the script I'm currently using with my network.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •