Kwehmann
Kwehmann

Reputation: 19

Extract IP and DHCP and set to variable from ipconfig /all. Batch

I am trying to extract the DHCP status and IP address from ipconfig /all and set it to a variable.

Can this be done?

Upvotes: 1

Views: 1303

Answers (2)

mr netlord
mr netlord

Reputation: 303

Well - I'm doing this with grep.

I don't know exactly what you need, but for example if you need the DHCP-server:

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

and for the local IP:

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

Both commands are working inside a batch file.

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 7095

It's easier using Netsh try this:

@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

Change local area connecton to suit your environment.

Upvotes: 2

Related Questions