Click here to Skip to main content
15,884,298 members
Articles / Operating Systems / Windows
Tip/Trick

Overwrite Your Network IPv4 Configuration from a Batch File

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
17 Jul 2018CPOL2 min read 23.6K   5   1
Easy way to get a specific IP address, mask, gate and DNS from a bat file

Introduction

I'm a robots and machines programmer that works for different companies.

Usually, each different project means connecting to the device to be programmed using a specific IP address.

Usually, there is no DHCP server and the same IP address to program the device is used while I'm programming at my customer company as when I'm programming at its customer place.

Of course, this means that for each project, I must remember the IP address I used, and set it before starting to program.

Things get funnier when I go back to the office and, when sometimes in the same day, I work at three different locations.

Years ago, IBM and afterwards, Lenovo offered the ThinkVantage Access Connections, a piece of software that easily allowed to store different network profiles and to activate them when needed. This made it super easy to switch from networks and was a real plus for people in the industrial sector.

Now, in Windows 10, this software is not working anymore.

I've been searching for different options and of course there are many, some free, some paid... but all of them store all the profiles in a folder, at the registry, ...

I was more interested in keeping each setting in each project folder. Of course, I asked at CP advice on that issue and @Nelek told me about netsh, the instruction I've decided to use to perform that network change easily.

Well, this is another tip that I hope will help other people and my future me too, just when I won't remember what I've done and why...

Background

You will need to know to save files, edit and create batch files, know a little bit about IP addresses...

Using the Code

The first thing to know is the name of the NIC you want to modify its data.  In order to do that, the easiest is to open a DOS window and execute this command:

netsh -c interface dump

This will write all the current configuration in the DOS window for you to copy it afterwards.

Once you know what is the name of the NIC, you want to modify its data, you can create a bat file with this content:

netsh interface ip set address name="Ethernet" address=192.168.42.42 mask=255.255.255.0 gate=192.168.42.1
netsh interface ip set dnsservers "Ethernet" static 8.8.8.8 validate=no
netsh interface ipv4 add dnsserver "Ethernet" address=8.8.4.4 index=2 validate=no

The first line of that file sets the IPv4 address, the mask and the gate for the "Ethernet" adapter.

The second line sets the first DNS for that adapter.

And the third line sets the second DNS.

The validate=no in the last two lines is only used to avoid receiving an error message after setting the DNS addresses.

Of course, you'll have to change the "Ethernet" name for "the name of the NIC you want to change", and it's not needed to say you'll have to modify the address, mask, gate and DNS values for your desired ones.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer robotecnik
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.

From 1998 I've been developing software applications for the automation market.

I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...

www.robotecnik.com[^] - robots, CNC and PLC programming

Comments and Discussions

 
QuestionYou are welcome Pin
Nelek17-Jul-18 22:07
protectorNelek17-Jul-18 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.