简体   繁体   English

通过 IP 地址查找网络接口 - Linux/Bash

[英]Find network interface by IP address - Linux/Bash

I'm wondering how I can query by IP address using sed, and it will show which interface name that is using it.我想知道如何使用 sed 按 IP 地址查询,它会显示使用它的接口名称。

For example..例如..

ipconfig -a | grep 10.0.0.10

I would expect it to come back with ETH0我希望它带着 ETH0 回来

You should use this comand :你应该使用这个命令:

ifconfig | grep -B1 "inet addr:10.0.0.10" | awk '$1!="inet" && $1!="--" {print $1}'

Hope this help !希望这有帮助!

ifconfig | grep -B1 10.0.0.10 | grep -o "^\w*"

If you want sed specific solution you may try this.如果你想要 sed 特定的解决方案,你可以试试这个。 Its little hard to digest how it works , but finally this combination works.很难消化它是如何工作的,但最终这种组合是有效的。

 ifconfig | sed -n '/addr:10.0.0.10/{g;H;p};H;x' | awk '{print $1}'

If you want to take it as an argument via script use "$1" or so instead if 10.0.0.10.如果您想通过脚本将其作为参数,请使用“$1”左右而不是 10.0.0.10。

Sed manual for reference : http://www.gnu.org/software/sed/manual/sed.html#tail sed 手册供参考: http : //www.gnu.org/software/sed/manual/sed.html#tail

ip -br -4 a sh | grep 10.0.0.10 | awk '{print $1}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM