2015-04-02 99 views
0

我试图做一个批处理文件,当使用将设置IP。我认为我达到了这一点。垮台是我希望它能够在我使用它时传递一个变量。例如SETIP 37.会将变量37插入IP中。批处理文件传递变量为ip

下面是我的代码

rem Network Settings for IP 

netsh interface ip set address name = "Local Area Connection" source = static addr = 192.168.10.(target for variable) mask = 255.255.255.0 

netsh interface ip set address name = "Local Area Connection" gateway = 192.168.10.1 gwmetric = 1 

netsh interface ip set dns name = "Local Area Connection" source = static addr = 192.168.10.1 

netsh interface ip show config 

pause 
+0

你尝试过什么?我会认为通过'“192.168.10。$ var $”'会工作... – 2015-04-02 13:07:26

+1

这将是'%var%',而不是'$ var $'。但是,你可以绝对简单地使用一个变量。 – SomethingDark 2015-04-02 13:16:00

回答

0

您可以轻松地摆脱这种创建一个命令行工具。保存下面的脚本为SetupIP.bat,然后作出它的调用(从提升的提示),像这样:

SetupIP 132 

上面会设置为192.168.10.132的IP地址的网络。

脚本源是上述受理的IP地址的最后一部分的命令行参数的命令:

@ECHO OFF 
REM Usage: 
REM SetupIP {'D' IP address component} 

REM Note that this command requires elevated rights, so it must be run as an administrator. 

REM Validate a value was entered. 
IF "%~1"=="" (
    ECHO No IP value was provided. 
    ECHO Enter only the last component of the IP address to configure. 
    GOTO :EOF 
) 

rem Network Settings for IP 

netsh interface ip set address name = "Local Area Connection" source = static addr = 192.168.10.%~1 mask = 255.255.255.0 

netsh interface ip set address name = "Local Area Connection" gateway = 192.168.10.1 gwmetric = 1 

netsh interface ip set dns name = "Local Area Connection" source = static addr = 192.168.10.1 

netsh interface ip show config