How to Set Static IP Address Using the Command Line
CHUONG K. NGUYEN – BSc., MCSEx2, MCSAx2, MCP, MCTS, MCITP, CCNA
The Microsoft Command for setting up static IP address on a Windows computer is as follows:
netsh interface ip set address name=”Ethernet” static 18.104.22.168 255.0.0.0 22.214.171.124
In the above command, 126.96.36.199 is the IP address to be set, 255.0.0.0 is the subnet mask, and 188.8.131.52 is the default gateway.
Before presenting the code, I am going to present a different code that reads in a String and spits it right out on the screen.
Save the following file as input.bat
@echo off echo Input Acceptance Batch Program echo ============================== set /p inputVariable=Please Enter a String: echo The input was "%inputVariable%"
Execute it and you will see the following results.
As you can see, the mechanism for obtaining the input is via this line:
set /p inputVariable=Please Enter a String:
And to spit it out, via this line:
echo The input was “%inputVariable%”
Using the same strategy for reading input and writing output, we will develop the TCP/IP setting application. We will read in three strings: the IP address, the subnet mask, and the default gateway.
After having these three values, we use the netsh command at the beginning of this lab to set up the TCP/IP values.
The code is as follows, save it as SetIP.bat and run it as Administrator:
@echo off echo Setting TCP/IP Addressing echo ========================= set /p ipAddress=Please Enter the IP address: set /p snm=Please Enter the Subnet Mask: set /p gateway=Please Enter the gateway: echo Setting TCP/IP as %ipAddress% %snm% through router %gateway%. netsh interface ip set address name="Ethernet" static %ipAddress% %snm% %gateway% echo Done!
To demonstrate execution, I will show you the before and after TCP/IP settings on the computer.