简体   繁体   中英

batch file IP configuration

I'm currently busy with writing a batch file, code below, check the brackets:

@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

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. (see brackets)

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. (see brackets)

This line will generate a syntax error - it has no target for the if clause.

IF "%choice%" == ""

and there is a lone ) with no mating (
but if there is one then you need delayed expansion with the way you have it arranged.

Here is how I would do it:

@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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM