Batch file 从ipconfig/all中提取IP和DHCP并设置为变量。批处理

Batch file 从ipconfig/all中提取IP和DHCP并设置为变量。批处理,batch-file,cmd,ipconfig,Batch File,Cmd,Ipconfig,我试图从ipconfig/all中提取DHCP状态和IP地址,并将其设置为变量 可以这样做吗?使用Netsh更容易些试试以下方法: @echo off setlocal enabledelayedexpansion for /f "skip=2 tokens=* delims=" %%a in ( 'netsh interface ipv4 show config name^="local area connection"' ) do ( set /a cnt+=1 if

我试图从ipconfig/all中提取DHCP状态和IP地址,并将其设置为变量


可以这样做吗?

使用
Netsh更容易些
试试以下方法:

@echo off
setlocal enabledelayedexpansion

for /f "skip=2 tokens=* delims=" %%a in (
  'netsh interface ipv4 show config name^="local area connection"'
) do (
    set /a cnt+=1 
    if !cnt! equ 3 (
      goto :break
    ) ELSE (echo(%%a
  )
)  
:break

更改本地连接以适应您的环境

嗯-我是用
grep
来做这件事的

我不知道您需要什么,但例如,如果您需要DHCP服务器:

for /F %%i in ('ipconfig -all ^| grep "DHCP Server" ^| grep -Eo '[0-9][0-9.]+'') do set DHCPServer=%%i
对于本地IP:

for /F %%j in ('ipconfig ^| grep "IPv4" ^| grep -Eo '[0-9][0-9.]+'') do set IpAdress=%%j

这两个命令都在批处理文件中工作。

是否会提前知道适配器的名称?