julycx
V2EX  ›  Windows

windows 平台如何获得当前默认使用的是哪个网络接口(interface)?

  •  
  •   julycx · Jan 2, 2025 · 2488 views
    This topic created in 675 days ago, the information mentioned may be changed or developed.

    兄弟们 windows 平台有用命令或代码知道当前默认网络接口是哪个吗 看了下都是要人自己识别啊 wlan or 以太网 vm 以太网 gpt 搜烂了

    linux 可以获得: p route | grep default | awk '{print $5}' 输出 eth0

    6 replies  •  2025-01-03 10:06:38 +08:00
    jonzhao
    jonzhao
    Jan 2, 2025❤️1
    1
    (Get-NetRoute | Where-Object DestinationPrefix -eq '0.0.0.0/0' | Get-NetIPInterface | Where-Object ConnectionState -eq 'Connected').InterfaceAlias
    lysShub
    lysShub
    Jan 2, 2025
    2
    route print -4 ,选缺省路由中 metric 最小的那条记录(可能不是第一条);


    api 可以调用 GetBestInterface ,这个在不是缺省路由时也能正常工作
    R4rvZ6agNVWr56V0
    R4rvZ6agNVWr56V0
    Jan 2, 2025
    3
    1 楼的 正解。
    xqzr
    xqzr
    Jan 2, 2025
    4
    少用管道
    ```shell
    ip --json r | jq -r '.[] | select(.dst == "default") | .dev'
    ip r | awk '/default/ {print $5}'
    ```
    benjaminliangcom
    benjaminliangcom
    Jan 3, 2025 via iPhone
    5
    @xqzr 这不也是管道吗
    julycx
    julycx
    OP
    Jan 3, 2025
    6
    @jonzhao 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   542 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:21 · PVG 06:21 · LAX 14:21 · JFK 17:21
    ♥ Do have faith in what you're doing.