批处理文件IP配置

问题描述 投票:0回答:2

我现在正忙于编写批处理文件,代码如下,检查括号:

@echo off 
cls
:start
echo On which network card do you want to perform the configuration?
NetSh Interface IPv4 Show Interfaces

(already opened the available network cards. Some are connected, some are not. So the code that should come here has to detect the idx, and check if the state of the network cards is connected. After that it should show all the connected and available network cards that are eligible to perform the configuration on, and when that is done, the user should be able to choose on which connected and available network cards he wants to perform this configuration. I was thinking about doing it with an array, but I don't know how to do this to be honest.)

echo.
echo. 
echo ======================
echo DHCP oor static?

echo press 1 for DHCP, 2 to configure automatically.
echo ======================
echo.
echo. 
set /p choice=Choose option 1 or 2:
if '%choice%'=='1' goto :dhcp
if '%choice%'=='2' goto :static

)
IF "%choice%" == ""
:dhcp
echo Batch file will close itself, since u chose for automatic settings.
goto end
:static
echo Chosen for static settings. Enter the ip adress.
set /p varip= 
echo Enter the subnetmask.
set /p varsm=
echo Enter the gateway.
set /p vargw=
echo Enter DNS.
set vardns1=

   (code for printing the IP, subnet, gateway and DNS to the selected network card. I'm also looking for the code to perform this action. That is all, thanks in advance. Keep in mind that the information that the user has entered above should be printed to the selected network card.)
 goto end

已经打开了可用的网卡。有些是连接的,有些则没有。因此,应该来的代码必须检测idx,并检查网卡的状态是否已连接。之后,它应该显示所有有资格执行配置的已连接和可用的网卡,一旦完成,用户应该能够选择他想要执行此配置的已连接和可用的网卡。我正在考虑用数组做这件事,但说实话,我不知道如何做到这一点。 (见括号)

用于将IP,子网,网关和DNS打印到所选网卡的代码。我也在寻找执行此操作的代码。这就是全部,在此先感谢。请记住,用户在上面输入的信息应该打印到选定的网卡。 (见括号)

windows batch-file configuration
2个回答
0
投票

我将如何做到这一点:

@echo off
setlocal enabledelayedexpansion

set cnt=0
for /f "tokens=1,4,5*" %%a in (
  'Netsh Int IPv4 show interfaces^| findstr /i /r /v "dis"^| more +3'
) do (
  set /a cnt+=1
  set "idx.!cnt!=%%a"
  set "idx.!cnt!.name=%%c %%d"
)

:Configure
echo.
echo Choose which adapter to configure.
for /l %%a in (1,1,!cnt!) do (
  if !idx.%%a.configured!==1 (
    echo !idx.%%a! -  !idx.%%a.name! - Configured
  ) ELSE (
    echo !idx.%%a! -  !idx.%%a.name!
  ) 
)
echo.
set /p "idx=Enter index to configure: "

for %%a in (%idx%) do (
    echo.
    echo. 
    echo ======================
    echo DHCP or static?

    echo press 1 for DHCP, 2 to configure manually.
    echo ======================
    echo.
    echo. 
    set /p "choice=Choose option 1 or 2: "
    if '!choice!'=='1' (
      echo You chose automatic settings for !idx.%%a.name!
      echo netsh int ipv4 set address %%a dhcp
      echo netsh int ipv4 dnsservers %%a dhcp
      set "idx.%%a.configured=1"
    ) ELSE (
      if '!choice!'=='2' (
        echo Chosen for static settings. 
        set /p varip=Enter the ip adress: 
        set /p varsm=Enter the subnetmask: 
        set /p vargw=Enter the gateway: 
        set /p vardns1=Enter DNS Server: 
        echo netsh int ipv4 set address %%a static !varip! !varsm! !vargw! 1
        echo netsh int ipv4 set dnsservers %%a static !vardns1! primary
        set "idx.%%a.configured=1"
      )
    )
)
set /p doagain="Would you like to configure another adapter? (Y/N): 
if /i '%doagain%'=='Y' (goto :configure) else Echo Configuration complete. 

1
投票

此行将生成语法错误 - 它没有if子句的目标。

IF "%choice%" == ""

并且有一个孤独的)没有交配的( 但如果有,那么你需要按照你安排的方式延迟扩张。

© www.soinside.com 2019 - 2024. All rights reserved.