I currently work in a distillery that mostly uses an ethernet based controls network that is dedicated for all automation equipment. To help save time, I created a batch file for each system at the distillery to be able to quickly change my computer's IP address when I am at different parts of the plant. I created this directory of batch files and placed them on our server so that our maintenance guys can access them. While this works good, it came to my mind that if multiple people attempt to go online in this manner that there will be conflicting IP addresses on the network.
I attempted to modify my batch file(s) to first ping an IP address (10.120.194.254) then based on the result, either alter the IPv4 "Local Area Connection" to that IP address or continue to the next available IP address (10.120.194.253)... (10.120.194.252)... and so on. My ideal goal is to perform this same procedure for 5 IP addresses then if all are unsuccessful, present a dialog box that displays a message to check the number of users on the network. Typically, IP addresses xxx.xxx.xxx.250-254 are used for programming on a network so this is the reasoning behind this. The probability of this many users on a network is low but it would be a nice feature to have for that "just in case scenario".
Below is my code for the original batch file to change the IP address for IPv4 "Local Area Connection".
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.254 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
I tried using the errorlevel function but could not get consistent results when pinging the network. Furthermore, every time I ping an address it polls 4 times then moves on to the next one. Is there a way to shorten this poll time? Below is my code for the batch file that I have been trying to get to work:
@echo off
ping "10.120.194.254"
IF %errorlevel%==0 GOTO IP_Attempt2
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.254 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt2
ping "10.120.194.253"
IF %errorlevel%==0 GOTO IP_Attempt3
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.253 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt3
ping "10.120.194.252"
IF %errorlevel%==0 GOTO IP_Attempt4
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.252 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt4
ping "10.120.194.251"
IF %errorlevel%==0 GOTO IP_Attempt5
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.251 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt5
ping "10.120.194.250"
IF %errorlevel%==0 GOTO Max_IP_Attempts
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.250 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:Max_IP_Attempts
echo Max attempts have been made with this batch file. Verify users on the network.
pause
exit /b
I have attached a snapshot of the CMD prompt window after running the batch file: