当前位置: 代码迷 >> 综合 >> 自动获取 网络资源 (ip Mac)
  详细解决方案

自动获取 网络资源 (ip Mac)

热度:2   发布时间:2023-12-08 05:03:12.0

@echo off
:::读取本机Mac地址
if exist ipconfig.txt del ipconfig.txt
ipconfig /all >ipconfig.txt

:::::::::读取本机ip地址
if exist IPAddr.txt del IPaddr.txt
find "IP Address" ipconfig.txt >IPAddr.txt
find "Subnet Mask" ipconfig.txt >MASKAddr.txt
for /f "skip=2 tokens=15" %%I in (IPAddr.txt) do set IP=%%I
for /f "skip=2 tokens=15" %%m in (MASKAddr.txt) do set MASKS=%%m

:::::::::读取网关地址
if exist GateIP.txt del GateIP.txt
find "Default Gateway" ipconfig.txt >GateIP.txt
for /f "skip=2 tokens=13" %%G in (GateIP.txt) do set GateIP=%%G

:::::::::读取DNS地址
if exist DNSServers.txt del DNSServers.txt
find "DNS Servers" ipconfig.txt >DNSServers.txt
for /f "skip=2 tokens=13" %%e in (DNSServers.txt) do set DNSIP=%%e

:::::::::设置本机IP地址
netsh interface ipv4 set address name="本地连接" source=static address=%IP% mask=%MASKS% gateway=%GateIP%
netsh interface ipv4 add dnsserver name="本地连接" address=%DNSIP% index=1

:::::::::清理文件
del /q ipconfig.txt
del /q IPAddr.txt
del /q MASKAddr.txt
del /q GateIP.txt
del /q DNSServers.txt
echo 任务完成