The design problem of some intranet dialing software, after dialing in an accidental situation can not automatically obtain DHCP to update the intranet IP, and later found that by manually releasing and regain IP can solve this problem, but each time before the operation also to see whether the IP address has been updated to the intranet address, if the update is not necessary to operate, Otherwise you have to manually release the update, every time you knock a bunch of commands is really troublesome, so intend to do batch processing.
The first problem encountered is how to obtain the IP address, of course, since the use of batch processing then need to use the existing command or command-line program, Windows System ipconfig command can obtain information such as IP address, if we only need IP address, then we must filter the information obtained, This requires the cooperation of tools such as findstr, combined with the sample final code found on the web as follows:
Copy Code code as follows:
for/f "tokens=2 delims=:"%%i in (' Ipconfig^|findstr "Address ') do set ip=%%i
SET ip=%ip:~1%
So the IP address is stored in the IP variable, and we can get it through the echo%ip%. Then to determine whether this IP address is an intranet segment, here we are simple, The known intranet are all 10 IP addresses that begin with, and the other IP segment is to remove 10 of the beginning of any IP segment, then the problem is simple, directly intercept the IP address of the first 3 and then judge is not 10. It's done, here I will paste the complete code for everyone to refer to it, write relatively simple:
Copy Code code as follows:
@ECHO off
for/f "tokens=2 delims=:"%%i in (' Ipconfig^|findstr "Address ') do set ip=%%i
SET ip=%ip:~1%
Set id=%ip:~0,3%
ECHO Current IP Address:%ip%
If not "%id%" = "goto fixipaddr"
ECHO It seems OK:-)
Goto END
: fixipaddr
ECHO Fix DHCP Configuration
ECHO Please wait ...
Ipconfig/release>nul 1>nul 2>nul
Ipconfig/renew>nul 1>nul 2>nul
ECHO all done
: End
PAUSE