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?
Thanks.
A little messy but should work:
/sbin/ifconfig | grep -B1 1.2.3.4 | awk '{print $1; exit}'
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}'