BAT batch to determine IP address and automatically disable enable NIC _dos/bat

Source: Internet
Author: User
Tags goto

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.