【问题标题】:Get Ip address of ethernet if not present then get it of WIFI adapter using Batch script如果不存在,则获取以太网的 IP 地址,然后使用批处理脚本获取 WIFI 适配器的 IP 地址
【发布时间】:2018-08-29 11:16:43
【问题描述】:

我有一个批处理脚本,它获取机器的 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%

【问题讨论】:

  • 您正在强制适配器名称,当您在其他设备上分发脚本时,硬编码不是一个好主意。您可以编写脚本以获取已配置设备的列表,从那里开始工作。命令wmic nic get NetConnectionID 应该会有所帮助
  • 感谢@GerhardBarnard 的快速回复。您能否详细说明在此脚本中如何以及在何处使用它?
  • 从 cmd 运行这个。 for /f "tokens=1,2 delims=:" %a in ('ipconfig ^| findstr IPv4') do echo %b 并告诉我返回了多少 IP。如果超过 1 个 IP,有些通常是 192 个地址,未使用,只有 1 个地址在您的 IP 范围内。假设以 `10.` 开头,我正确吗?
  • 我有一个场景,我想获取已启用网络的 IP 地址。我相信它们可以是空白的。
  • 那么连接的网络IP范围总是以相同的整数开头?不管是wifi还是lan,都是10开头的,比如?

标签: batch-file


【解决方案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!

【讨论】:

  • 对我尝试过的每个案例都非常完美。非常感谢@Gerhard。
  • 完美,我的荣幸。
  • 感谢@GerhardBarnard 这个脚本极大地帮助了我设置 wsl2 桌面环境
【解决方案2】:

使用WMIC

@echo off

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

【讨论】:

    【解决方案3】:

    你可以试试这个批处理脚本:

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

    【讨论】:

    • 我只想要“以太网适配器以太网”的 IP 地址,如果找不到,请获取 WIFI 适配器。
    猜你喜欢
    • 2011-04-21
    • 2018-08-06
    • 2023-04-08
    • 1970-01-01
    • 2016-10-24
    • 2013-05-19
    • 2022-10-12
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多