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.
You can follow a path to a device if you know its IP address by using the tracert command at the command prompt (cmd). Open a Command Prompt window and type in tracert followed by the IP address that you know. The output will show each router that has a connection to that device will pass through.
How do I find the port number of a specific IP address? All you have to do is type “netstat -a” on Command Prompt and hit the Enter button. This will populate a list of your active TCP connections. The port numbers will be shown after the IP address and the two are separated by a colon.
You can use the ifconfig command or ip command with grep command and other filters to find out an IP address assigned to eth0 and display it on screen.
Replace 127.0.0.1 with the ip address you want to get the interface info for
ifconfig | grep 127.0.0.1 -B1 | grep Link | cut -f 1 '-d '
If you also want to identify the physical port on the machine, run
ethtool -p $OUTPUT_OF_FIRST_COMMAND
It will blink the light on the ethernet card associated with that interface
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}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With