:: ==============================
:: code by fireghost57
:: modify: 2013.08.20
:: ==============================
@echo off
title 导出网络配置
setlocal enabledelayedexpansion %/*开启此项后可在截取字符串时保存截取内容*/%
netsh wlan set hostednetwork mode=disallow %/*承载网络模式不禁止的话不可使用(默认都没有承载模式)*/%
cls
:LISTEN
cls
set f_Hostname=Name
set f_Desc=Description
set f_IP=IP Address
set f_MAC=Physical Address
set f_Mask=Subnet Mask
set f_Gateway=Default Gateway
set f_DNS=DNS Servers
:: 查找主机名
set /a g=0
set /a v=0
set /a line=0
for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr /c:"%f_Hostname%"') do (
set /a g+=1
set /a v+=1
if !g! equ !v! (
set /a line+=1
set "Name_host=%%i"
)
)
:: 语言跳转
set /a language=0
if %line% EQU 0 (
set /a language=1
goto LISTCN
)
goto PRINTINFO
:LISTCN
cls
set f_Hostname=主机名
set f_Desc=描述
set f_IP=IPv4 地址
set f_MAC=物理地址
set f_Mask=子网掩码
set f_Gateway=默认网关
set f_DNS=DNS 服务器
:: 查找主机名
set /a g=0
set /a v=0
set /a line=0
for /f "tokens=2* delims=:" %%i in ('ipconfig/all^|findstr /c:"%f_Hostname%"') do (
set /a g+=1
set /a v+=1
if !g! equ !v! (
set /a line+=1
set "Name_host=%%i"
)
)
goto PRINTINFO
:: 生成用户信息
:PRINTINFO
:: 过滤字符串
set Name_host=%Name_host: =%
:: 输出信息
ipconfig /all>["%Name_host%"].cfg
goto GETDESC
:: 获取网卡描述
:: findstr /n 可以在搜索时添加行号,因此截取的行号保存在相应搜索变量中
:GETDESC
:: 获取网卡描述信息在配置文件中的位置
set /a line=0
for /f "tokens=1 delims=:" %%i in ('findstr /n /c:"%f_Desc%" [%Name_host%].cfg') do (
set /a line+=1
set Search_!line!=%%i
)
:: 存储网卡描述
set /a g=0
set /a v=0
set /a line=0
for /f "tokens=2 delims=:" %%i in ('findstr /c:"%f_Desc%" [%Name_host%].cfg') do (
set /a g+=1
set /a v+=1
:: 输出网卡信息
if !g! equ !v! (
echo !g!.%%i
set /a line+=1
)
set Desc_!line!=%%i
)
:: 用户选择
echo 发现%line%条网卡信息.
if %line% == 1 (
set /a num=1
set /a next_num=num+1
) else (
set /p num=请输入要处理的网卡序号:
set /a next_num=num+1
)
if %num% LEQ %line% (goto GETNETINFO)
echo 输入错误,请重新输入.
pause>nul
cls
goto GETDESC
:: 获取网络信息
:: findstr /n 可以在搜索时添加行号,因此截取的行号保存在相应搜索变量中
:GETNETINFO
:: 查找MAC信息
set "MAC_host= "
for /f "tokens=1,3 delims=:" %%i in ('findstr /n /c:"%f_MAC%" [%Name_host%].cfg') do (
if !num! neq !line! (
:: 不是最大值
if !Search_%num%! leq %%i (
if %%i leq !Search_%next_num%! (
set MAC_host=%%j
)
)
) else (
:: 是最大值
if !Search_%num%! leq %%i (
set MAC_host=%%j
)
)
)
:: 查找IP信息
set "IP_host= "
for /f "tokens=1,3 delims=:" %%i in ('findstr /n /c:"%f_IP%" [%Name_host%].cfg') do (
if !num! neq !line! (
:: 不是最大值
if !Search_%num%! leq %%i (
if %%i leq !Search_%next_num%! (
set IP_host=%%j
)
)
) else (
:: 是最大值
if !Search_%num%! leq %%i (
set IP_host=%%j
)
)
)
:: 查找掩码信息
set "Mask_host= "
for /f "tokens=1,3 delims=:" %%i in ('findstr /n /c:"%f_Mask%" [%Name_host%].cfg') do (
if !num! neq !line! (
:: 不是最大值
if !Search_%num%! leq %%i (
if %%i leq !Search_%next_num%! (
set Mask_host=%%j
)
)
) else (
:: 是最大值
if !Search_%num%! leq %%i (
set Mask_host=%%j
)
)
)
:: 查找网关信息
set "Gateway_host= "
for /f "tokens=1,3 delims=:" %%i in ('findstr /n /c:"%f_Gateway%" [%Name_host%].cfg') do (
if !num! neq !line! (
:: 不是最大值
if !Search_%num%! leq %%i (
if %%i leq !Search_%next_num%! (
set Gateway_host=%%j
)
)
) else (
:: 是最大值
if !Search_%num%! leq %%i (
set Gateway_host=%%j
)
)
)
:: 查找DNS信息
set "DNS_host= "
for /f "tokens=1,3 delims=:" %%i in ('findstr /n /c:"%f_DNS%" [%Name_host%].cfg') do (
if !num! neq !line! (
:: 不是最大值
if !Search_%num%! leq %%i (
if %%i leq !Search_%next_num%! (
set DNS_host=%%j
)
)
) else (
:: 是最大值
if !Search_%num%! leq %%i (
set DNS_host=%%j
)
)
)
goto SHOW
:SHOW
:: 过滤字符串
set Name_host=%Name_host: =%
set IP_host=%IP_host: =%
set MAC_host=%MAC_host: =%
set Mask_host=%Mask_host: =%
set Gateway_host=%Gateway_host: =%
set DNS_host=%DNS_host: =%
if %language% == 1 (
set IP_host=%IP_host:(=%
set IP_host=%IP_host:首选=%
set IP_host=%IP_host:)=%
)
:: 显示信息
echo Name_host :[%Name_host%]
echo IP_host :[%IP_host%]
echo MAC_host :[%MAC_host%]
echo Mask_host :[%Mask_host%]
echo Gateway_host :[%Gateway_host%]
echo DNS_host :[%DNS_host%]
:: =========== 生成配置 ===========
set "ipconfig_dir=ipconfig.ini"
echo [查找主机]> %ipconfig_dir%
echo IP_find = >> %ipconfig_dir%
echo MAC_find = >> %ipconfig_dir%
echo [修改参数]>> %ipconfig_dir%
echo HOSTNAME_set = %Name_host%>> %ipconfig_dir%
echo IP_set = %IP_host%>> %ipconfig_dir%
echo MAC_set = %MAC_host%>> %ipconfig_dir%
echo MASK_set = %Mask_host%>> %ipconfig_dir%
echo GATEWAY_set = %Gateway_host%>> %ipconfig_dir%
echo DNS1_set = %DNS_host%>> %ipconfig_dir%
echo DNS2_set = >> %ipconfig_dir%
echo 用户网络信息已保存在 [%Name_host%].cfg 文件内,配置信息保存在 ipconfig.ini 文件内
pause>nul
导出网络配置.bat
最新推荐文章于 2022-11-17 15:21:40 发布