Dynamic binding of IP and MAC address batch processing XP and win7 available (reuse idea of software development)

Source: Internet
Author: User

Mr Mi gave a batch processing file, which is very simple. After our adaptation, we can now use batch processing in both Windows 7 and XP. Although the Code segment is quite simple, it reflects the important idea of software development-reuse !!

My generation needs to be improved...

The SET command cannot be used if and else appear in the IF and else for a whole day. You have to use Goto, and cannot bind ARP-s commands in win7. You need to use advanced binding commands.

 


@ Echo off
If exist ipconfig.txt del ipconfig.txt
Ipconfig/All> ipconfig.txt
Setlocal enabledelayedexpansion
If exist phyaddr.txt del phyaddr.txt

Rem searches for specified rows
Set n = 0
For/F "tokens = *" % I in (ipconfig.txt) Do (
Set/a n + = 1
If! N! Equ 10 echo % I> phyaddr.txt
)
For/F "tokens = 1" % C in (phyaddr.txt) do set cont = % C
If % cont % = physical address .(
Echo win7
Goto win7
) Else (
Echo XP
Goto WINXP
)
: Win7
If exist phyaddr.txt del phyaddr.txt
Rem searches for specified rows
Set n = 0
For/F "tokens = *" % I in (ipconfig.txt) Do (
Set/a n + = 1
If! N! Equ 10 echo % I> phyaddr.txt
)

Rem finds the local MAC address and assigns a value to the variable Mac.
For/F "tokens = 15" % m in (phyaddr.txt) do set MAC = % m

If exist ipaddr.txt del ipaddr.txt
Find "IPv4 address" ipconfig.txt> ipaddr.txt
Rem finds the local IP Address
For/F "Skip = 2 tokens = 16" % I in (ipaddr.txt) do set IP = % I
Set Var = % IP :~ 0, 14%
: @ Echo on
ARP-S % var % Mac %
If exist already ip.txt del already ip.txt
Find "Default Gateway" ipconfig.txt> gateip.txt

Rem finds the IP address of the Gateway
For/F "Skip = 2 tokens = 15" % G in (ip.txt) do set destination IP = % G
If exist gatemac.txt del gatemac.txt
ARP-A % slave IP %> gatemac.txt

Rem finds the MAC address of the Gateway
For/F "Skip = 3 tokens = 2" % H in (gatemac.txt) do set hosts MAC = % H

Netsh interface IPv4 show neighbors> ipv4.txt
Set n = 0
For/F "tokens = *" % I in (%4.txt) Do (
Set/a n + = 1
If! N! Equ 1 echo % I> ipv4.txt
)
For/F "tokens = 2" % C in (%4.txt) do set contd = % C
Set vard = % contd :~ 0, 2%
Netsh interface IPv4 set neighbors % vard % gateip % gatemac %
Pause

If exist ipconfig.txt del ipconfig.txt
If exist phyaddr.txt del phyaddr.txt
If exist ipaddr.txt del ipaddr.txt
If exist gatemac.txt del gatemac.txt
If exist already ip.txt del already ip.txt
If exist example 4.txt del example 4.txt
Exit

: WINXP
Echo executes XP

If exist ipconfig.txt del ipconfig.txt
Ipconfig/All> ipconfig.txt
If exist phyaddr.txt del phyaddr.txt
Find "physical address" ipconfig.txt> phyaddr.txt
For/F "Skip = 2 tokens = 12" % m in (phyaddr.txt) do set MAC = % m
If exist ipaddr.txt del ipaddr.txt
Find "ip address" ipconfig.txt> ipaddr.txt
For/F "Skip = 2 tokens = 15" % I in (ipaddr.txt) do set IP = % I
: @ Echo on
ARP-S % IP % Mac %
If exist already ip.txt del already ip.txt
Find "Default Gateway" ipconfig.txt> gateip.txt
For/F "Skip = 2 tokens = 13" % G in (ip.txt) do set destination IP = % G
If exist gatemac.txt del gatemac.txt
ARP-A % slave IP %> gatemac.txt
For/F "Skip = 3 tokens = 2" % H in (gatemac.txt) do set hosts MAC = % H
ARP-S % gateip % gatemac %

If exist ipconfig.txt del ipconfig.txt
If exist phyaddr.txt del phyaddr.txt
If exist ipaddr.txt del ipaddr.txt
If exist gatemac.txt del gatemac.txt
If exist already ip.txt del already ip.txt

Exit

 

I am not very familiar with batch processing commands. I used a very stupid method.

 

New Blog

 

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.