2015-04-02 31 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