Sam Brooks
Sam Brooks

Reputation: 143

Get Ip address of ethernet if not present then get it of WIFI adapter using Batch script

I have a batch script which gets the IP address of the machine and if not present then retrieve IP address of WIFI adapter. It works fine for Ethernet Adapter, however when it finds for WIFI adapter it works on some systems but fails on others as I found the names of WIFI adapters are different on different System. I don't know much about batch scripting. Here is the script I tried.

@echo off
setlocal enabledelayedexpansion
::just a sample adapter here:
set "adapter=Ethernet adapter Ethernet"
set adapterfound=false
echo Network Connection Test
echo %new%
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig`) do (
    set "item=%%f"
    if /i "!item!"=="!adapter!" (
        echo found
        set adapterfound=true
    ) else if not "!item!"=="!item:IPv4 Address=!" if "!adapterfound!"=="true" (
        set _IPaddr=%%g
        echo Your IP Address is: %%g
        goto :break
        rem set adapterfound=false
        rem echo not found
    )
    rem echo adapterfound
)

:break
if "!adapterfound!"=="false"  (

    :: sometimes I get another name like "adapter=Wireless LAN adapter Wi-Fi 2"

    set "adapter=Wireless LAN adapter Wireless Network Connection"
    set adapterfound=false
    echo Network Connection Test
    for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig`) do (
        set "item=%%f"
        if /i "!item!"=="!adapter!" (
            echo found
            set adapterfound=true
        ) else if not "!item!"=="!item:IPv4 Address=!" if "!adapterfound!"=="true" (
            set _IPaddr=%%g
            echo Your IP Address of WIFI is: %%g
            set adapterfound=false
        )
    )
)
for /f "tokens=* delims= " %%a in ("%_IPaddr%") do set _IPaddr=%%a
echo %_IPaddr%

Upvotes: 5

Views: 821

Answers (3)

user7818749
user7818749

Reputation:

Please give this a try:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3 delims={,}" %%a in ('"wmic nicconfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do set gate_test=%%~a
set gate_test=!gate_test: =!
for /f "tokens=1-3 delims=^." %%i in ("!gate_test!") do set range=%%i.%%j.%%k
for /f "tokens=1,2 delims=:" %%l in ('ipconfig ^| findstr IPv4') do (
   set ip=%%m
   set ip=!ip: =!
for /f "tokens=1-3 delims=^." %%n in ("!ip!") do set iprange=%%n.%%o.%%p
if !iprange! == !range! set ipaddress=!ip!
)
)
echo My IP Address is !ipaddress!

Upvotes: 2

Hackoo
Hackoo

Reputation: 18827

You can give this batch script a try:

@echo off
Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017
mode con cols=80 lines=5 & Color 9E
echo( & echo(
echo   Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ...
Set "LogFile=%~dpn0.txt"
@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
    set "LAN_IP=%%a"
)

for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A


@For /f %%a in ('getmac /NH /FO Table') do  (
    @For /f %%b in ('echo %%a') do (
        If /I NOT "%%b"=="N/A" (
            Set "MY_MAC=%%b"
        )
    )
)
    Cls
    echo(
    echo                My Private LAN IP       : %LAN_IP%
    echo                My External Public IP   : %ExtIP%
    echo                MAC Address             : %MY_MAC%

(
    echo My Private LAN IP      : %LAN_IP%
    echo My External Public IP  : %ExtIP%
    echo MAC Address            : %MY_MAC%

)>"%LogFile%"
Timeout /T 5 /NoBreak>nul
Start "" "%LogFile%"

Upvotes: 0

SachaDee
SachaDee

Reputation: 9545

Using WMIC :

@echo off

for /f "skip=1 tokens=1 delims={," %%a in ('wmic nicconfig where "IPEnabled  = True" get ipaddress ^| findstr "."') do echo %%~a

Upvotes: 1

Related Questions