获取以太网的Ip地址(如果不存在),然后使用“批处理”脚本获取WIFI adapater的IP地址

时间:2018-08-29 11:16:43

标签: batch-file

我有一个批处理脚本,该脚本可以获取机器的IP地址,如果不存在,则可以检索WIFI适配器的IP地址。它对于以太网适配器可以正常工作,但是当它找到WIFI适配器时,它可以在某些系统上运行,但无法运行其他人,因为我发现不同系统上的WIFI适配器名称不同。我对批处理脚本了解不多。 这是我尝试过的脚本。

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

3 个答案:

答案 0 :(得分:1)

请尝试一下:

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

答案 1 :(得分:1)

使用determineLoggedUser()

WMIC

答案 2 :(得分:0)

您可以尝试以下批处理脚本:

@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%"