我不明白你为什么重新发明已经存在数十倍。用switch network settings
的全球网络搜索引擎进行搜索,您会发现许多用于在网络接口的网络设置之间切换的工具。
也可以在网络接口上设置多个IP地址,子网掩码,网关和DNS服务器,这样当使用总是像办公室网络和家庭网络一样的网络时,不必一直切换网络设置。
但是,这里是一个简单的批处理代码,用于加载和保存环境变量的值。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SaveData="
set "IpMod_IPAddress="
set "IpMod_Subnet="
set "IpMod_Gateway="
set "IpMod_Dns="
if exist "IpMod_Data.txt" for /F "usebackq tokens=1,2* delims=_=" %%A in ("IpMod_Data.txt") do if "%%A" == "IpMod" set "%%A_%%B=%%C"
if not "!IpMod_IPAddress!" == "" goto ValidateIpAddress
:InputIpAddress
echo/
set "SaveData=1"
set "IpMod_IPAddress=10.10.3.5"
set /P "IpMod_IPAddress=Enter IP address (default: %IpMod_IPAddress%): "
:ValidateIpAddress
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputIpAddress on invalid input.
if not "!IpMod_Subnet!" == "" goto ValidateSubnet
:InputSubnet
echo/
set "SaveData=1"
set "IpMod_Subnet=255.255.0.0"
set /P "IpMod_Subnet=Enter subnet mask (default: %IpMod_Subnet%): "
:ValidateSubnet
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputSubnet on invalid input.
if not "!IpMod_Gateway!" == "" goto ValidateGateway
:InputGateway
echo/
set "SaveData=1"
set "IpMod_Gateway=10.10.1.1"
set /P "IpMod_Gateway=Enter gateway IP (default: %IpMod_Gateway%): "
:ValidateGateway
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputGateway on invalid input.
if not "!IpMod_Dns!" == "" goto ValidateDns
:InputDns
echo/
set "SaveData=1"
set "IpMod_Dns=10.10.1.1"
set /P "IpMod_Dns=Enter DNS IP (default: %IpMod_Dns%): "
:ValidateDns
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputDns on invalid input.
if "%SaveData%" == "1" set IpMod_ >"IpMod_Data.txt"
echo/
echo The network data are:
echo/
echo IP address: !IpMod_IPAddress!
echo Subnet mask: !IpMod_Subnet!
echo Gateway IP: !IpMod_Gateway!
echo DNS IP: !IpMod_Dns!
echo IP Address will now change to the static address of "%IpMod_IPAddress%"
%SystemRoot%\System32\netsh.exe int ipv4 set address name="Local Area Connection" source=static address="%IpMod_IPAddress%" mask="%IpMod_Subnet%" gateway="%IpMod_Gateway%"
echo IP Address Changed...
%SystemRoot%\System32\timeout.exe /T 2 /NOBREAK >nul
echo Subnet Changed...
%SystemRoot%\System32\timeout.exe /T 2 /NOBREAK >nul
echo Gateway Changed...
%SystemRoot%\System32\timeout.exe /T 2 /NOBREAK >nul
echo Dns Changed...
%SystemRoot%\System32\netsh.exe int ip set dnsservers name="Local Area Connection" source=static address="%IpMod_Dns%" validate=no
echo IP address is now changed. You can now close this window.
endlocal
pause >nul
我从C++代码,我写了我的公司检查用户输入的网络设置检测无效的输入尽可能我会假设知道你有这个代码完成整个任务的0.5%。祝好运编码剩下的99.5%。
为了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
setlocal /?
timeout /?
netsh /?
netsh int /?
netsh int ip /?
netsh int ipv4 /?
- 等了
netsh
阅读也是微软文章关于Using Command Redirection Operators。
还可以看看Assigning IP from batch script上的答案。
[复制](https:// stackoverflow。com/q/37106858/2152082) – Stephan
如果您在开始时设置了“@ECHO OFF”,那么在每个“echo”完全冗余之前的“@” –