我怎样才能获取给定的IP地址的以太网端口? [英] How can I fetch the ethernet port given the ip address?

查看:540
本文介绍了我怎样才能获取给定的IP地址的以太网端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash脚本来获取其IP地址,我知道接口的以太网端口。我需要从使用ifconfig抓住这个,但似乎没有能够找出如何去做。任何想法?

I am trying to write a bash script to fetch the ethernet port of an interface whose IP address I know. I need to grab this from ifconfig but can't seem to be able to figure out how to go about it. Any ideas?

感谢。

推荐答案

一个有点混乱,但应该工作:

A little messy but should work:

/sbin/ifconfig | grep -B1 1.2.3.4 | awk '{print $1; exit}'

另外,您可以使用 IP 的命令,它与<$使用时C $ C> -o | -oneline 选项,是一个更容易解析。例如:

Optionally, you could use the ip command which, when used with the -o|-oneline option, is a lot easier to parse. For example

ip -o addr | awk '/1.2.3.4/{print $2}'

这篇关于我怎样才能获取给定的IP地址的以太网端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆