簡體   English   中英

批處理文件IP配置

[英]batch file IP configuration

我目前正忙於編寫批處理文件(下面的代碼),請檢查方括號:

@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打印到所選網卡的代碼。 我也在尋找執行此操作的代碼。 就這樣,謝謝。 請記住,用戶在上面輸入的信息應打印到所選的網卡上。 (請參閱方括號)

該行將產生語法錯誤-它沒有if子句的目標。

IF "%choice%" == ""

沒有交配的孤獨) (
但如果有一個,則需要按照安排的方式延遲擴展。

這是我的方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM