从批处理文件的IP配置 [英] IP-Configuration from Batch file

查看:248
本文介绍了从批处理文件的IP配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@echo off

set /p option=(1) Edit IP (2) Enable DHCP:

if %option%==1 (

set /p IP=New IP-Address:
set /p MASK=New Network Mask:
set /p GATE=New Gateway Address:

netsh interface ip set address name="LAN" static %IP% %MASK% %GATE% 1
)

if %option%==2 (

netsh interface ip set address name="LAN" source=dhcp
)

pause

该计划的一部分,DHCP工作得很好。 NIC的名称是LAN。我曾尝试使用和不使用1 paramenter上设置的IP。我也曾尝试用不同的变量名。

The DHCP part of the program works just fine. The NIC's name is "LAN". I have tried with and without the 1 paramenter on the set IP. I have also tried with different variable names.

输入一个有效的IPv4地址后,我得到的错误,一个子网掩码和网关地址是:

The error I get after entering a valid ipv4 address, a subnetmask and a gateway address is:

无效的地址参数< 1>:必须是有效的IPv4地址

Invalid Address Parameter <1>: Must be a valid IPv4-Address

推荐答案

您的问题被延迟扩展。所有变量code(括号内code)的块中读取的块开始执行之前,在变量中的值替代。如果一个变量在块内改变,新的价值不能被检索,因为所有读取操作变量与值替换。要解决,使在需要延迟读延迟扩展,并在变量,改变的%VAR%sintax有!无功!以指示解析器延迟读取直到执行时

Your problem is delayed expansion. All variable reads inside a block of code (code inside parenthesis) are replaced with the value in the variable before the block starts to execute. If a variable is changed inside the block, that new value can not be retrieved, as all reads to variables were replaced with values. To solve, enable delayed expansion and, in variables where delayed read is needed, change %var% sintax with !var! to indicate the parser to delay the read until the time of execution.

@echo off

set /p option=(1) Edit IP (2) Enable DHCP:

if %option%==1 (
    set /p IP=New IP-Address:
    set /p MASK=New Network Mask:
    set /p GATE=New Gateway Address:

    setlocal enabledelayedexpansion
    netsh interface ip set address name="LAN" static !IP! !MASK! !GATE! 1
    endlocal
)

if %option%==2 (
    netsh interface ip set address name="LAN" source=dhcp
)

pause

这篇关于从批处理文件的IP配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆