【问题标题】:How to find network interface name from IP Address如何从 IP 地址中查找网络接口名称
【发布时间】:2018-04-11 17:30:13
【问题描述】:

什么是终端命令或如何从 IP 地址中找到网络接口名称(例如 en0、en1、en3、en8 等)。 我有一个 IP 地址,需要找到该 IP 分配给 macOS 中的哪个网络接口。

【问题讨论】:

  • 在 macOS 中,但您标记了 Ubuntu 和 Linux。它是哪一个?我猜ifconfig 可能会给你一些结果?
  • 是的.. ifconfig 会显示,但也会显示其他内容。但我只想要接口名称 en1,en2 例如。 Xxxxx 10.20.30.40 结果应该是 en1 或 en2

标签: macos shell terminal command-line-interface


【解决方案1】:

正确的做法是:

route -n get "yourIPhere" | awk '/interface: / {print $2}'

【讨论】:

    【解决方案2】:

    我现在不在电脑前,而是类似

    ifconfig | grep -b1 "youriphere" | awk 'NR==1{print $1}'
    

    应该让你在球场上。

    【讨论】:

    • 不,它不是.. iost 显示结果 - inet6 如果我删除 awk,它将显示 - inet6 xxx::xx:xxxxx:xxxxxxx prefixlen 64 secure scopeid 0xe inet 20.30.40.540 网络掩码 0xffffffe00 广播 100.100.100.100 它没有给出整行
    【解决方案3】:

    其他答案大多忽略一个问题。可能会匹配到类似的IP,例如,如果网络接口是172.16.1.100,则IP为72.16.1.10也可以匹配。
    这是我的解决方案:

    ifconfig | grep -PB1 "[: ]72.16.1.10[^\d]" | grep -o "^\w*"
    or
    ifconfig | grep -PB1 "[: ]72.16.1.10[^\d]" | awk -F'[: ]' 'NR==1{print $1}'
    

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 1970-01-01
      • 2017-01-25
      • 2012-09-10
      • 2013-06-21
      • 1970-01-01
      • 2020-07-08
      • 2012-04-09
      • 1970-01-01
      相关资源
      最近更新 更多