V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
julycx
V2EX  ›  Windows

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

  •  
  •   julycx · Jan 2, 2025 · 1990 views
    This topic created in 490 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
        1
    jonzhao  
       Jan 2, 2025   ❤️ 1
    (Get-NetRoute | Where-Object DestinationPrefix -eq '0.0.0.0/0' | Get-NetIPInterface | Where-Object ConnectionState -eq 'Connected').InterfaceAlias
    lysShub
        2
    lysShub  
       Jan 2, 2025
    route print -4 ,选缺省路由中 metric 最小的那条记录(可能不是第一条);


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