到目前为止,我有一个批处理脚本,可以将IP地址更改为静态地址。现在,变量在批处理文件中定义。
我正在寻找的是添加一个IF
语句,用于检查变量值是否存在文件,在这种情况下,批处理文件会询问变量的值,然后保存变量'将文件值设置为稍后再次设置IP地址。
到目前为止,我有这个:
@ECHO OFF
SET IPAddress=10.10.3.5
SET Subnet=255.255.0.0
SET Gateway=10.10.1.1
SET Dns=10.10.11.87
@ECHO IP Address will now change to the static address of "%IPAddress%"
netsh int ipv4 set address name="Local Area Connection" source=static address="%IPAddress%" mask="%Subnet%" gateway="%Gateway%"
@ECHO IP Address Changed...
timeout /T 2 /NOBREAK > nul
@ECHO Subnet Changed...
timeout /T 2 /NOBREAK > nul
@ECHO Gateway Changed...
timeout /T 2 /NOBREAK > nul
@ECHO DNS Changed...
netsh int ip set dnsservers name="Local Area Connection" source=static address="%Dns%" validate=no
@Echo IP address is now changed. You can now close this window.
pause > nul
答案 0 :(得分:0)
我不明白为什么你重新发明已存在数十次。使用switch network settings
的万维网搜索引擎进行搜索,您会发现许多用于在网络接口的网络设置之间切换的工具。
还可以在网络接口上设置多个IP地址,子网掩码,网关和DNS服务器,这使得在使用办公网络和家庭网络等相同网络时无需始终切换网络设置。 / p>
但是,这是一个用于加载和保存环境变量值的简单批处理代码。
@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
另请阅读Microsoft有关Using Command Redirection Operators的文章 并在Assigning IP from batch script上查看答案。