2017-06-28 46 views
0

到目前为止,我有一个批处理脚本,将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 
+3

[复制](https:// stackoverflow。com/q/37106858/2152082) – Stephan

+0

如果您在开始时设置了“@ECHO OFF”,那么在每个“echo”完全冗余之前的“@” –

回答

0

我不明白你为什么重新发明已经存在数十倍。用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上的答案。

相关问题