LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Find out which Interface an IP address is attached to (https://www.linuxquestions.org/questions/linux-networking-3/find-out-which-interface-an-ip-address-is-attached-to-597855/)

helptonewbie 11-07-2007 02:38 PM

Find out which Interface an IP address is attached to
 
Hello,

I would like to know if there is a command or something that means something like:
Code:

command 192.168.0.1
which could result in telling me what network interface card that IP address is assigned to on the computer. Meaning is it assigned to eth0 or eth1 or perhaps bond0 or what ever really. I know i can write a script for it that does something like:

Code:

#get line number of ifconfig ipaddress is found on
line=`ifconfig | grep -n 'inet addr:192.168.1.2 ' | awk {'print $1'} | cut -d: -f1`
#go up one line from the line number got from ifconfig
((line--))
#using that line number if ifconfig is a standard format then this will now print the interface
ifconfig | sed -n "${line}p" | awk {'print $1'}

Which currently returns eth0, which is fine but if the ip address wasn't on the line below where eth0 is stated which it is at the moment then this command will fail so i wondered is there a better way to find out?? The concern being does the output of ifconfig change even slightly from distro to distro or is it always laid out in the same format?

Cheers

0.o 11-07-2007 03:29 PM

Quote:

Originally Posted by helptonewbie (Post 2951320)
Hello,

I would like to know if there is a command or something that means something like:
Code:

command 192.168.0.1
which could result in telling me what network interface card that IP address is assigned to on the computer. Meaning is it assigned to eth0 or eth1 or perhaps bond0 or what ever really. I know i can write a script for it that does something like:

Code:

line=`ifconfig | grep -n 'inet addr:192.168.1.2 ' | awk {'print $1'} | cut -d: -f1`
((line--))
ifconfig | sed -n "${line}p" | awk {'print $1'}

Which currently returns eth0, which is fine but if the ip address wasn't on the line below where eth0 is stated which it is at the moment then this command will fail so i wondered is there a better way to find out?? The concern being does the output of ifconfig change even slightly from distro to distro or is it always laid out in the same format?

Cheers

replace the ip with $1.

helptonewbie 11-07-2007 03:52 PM

i'm sorry but could you expand a little?

helptonewbie 11-08-2007 06:21 AM

does anybody else have more info please??
Quote:

I would like to know if there is a command or something that means something like:
Code:

command [args] 192.168.0.1

which could result in telling me what network interface card that IP address is assigned to on the computer. Meaning is it assigned to eth0 or eth1 or perhaps bond0 or what ever really.

AND
The concern being does the output of ifconfig change even slightly from distro to distro or is it always laid out in the same format??????? even in roughly some different versions of ifconfig, as long as the ip address is always on the line below the eth0 for example then my script will work fine?


pixellany 11-08-2007 06:52 AM

O.o was just saying that you should pass an argument to the script instead of writing the IP inside....

I think the output of ifconfig will be pretty consistent across distros.

Assuming the ethX is always on the preceding line, you could do:
ifconfig | grep -B1 $1 | grep eth
this gives you just the line containing "eth" (If for any reason it might show up 2-3 lines back, then just use a higher number in -B)

Then you can just use awk to strip off the first field.

lsteacke 11-08-2007 08:10 AM

I would suggest just writing a more verbose script. Passing an argument instead of the ip is a great way to start.

helptonewbie 11-08-2007 11:30 AM

sorry yes must have confused people a bit, $1 will read in the arguments off a command thats executed and because i used command 192...... you guy's think i'm trying to write a script. The "command [args] 192..." was asking if there is already a command that you can use that will do the job for me (probably should have said "command [parameters] ipaddress}"), if not then i was going to use my own script as shown.

Thanks and judging by replies i'm guessing there's not something that can do this already unless i've confussed people so much know you may understand what i'm trying to say and do know of something??

Sorry

ps - thanks pixellany, i was going to say in all the different distro's i've used so far, i've not noticed the output of ifconfig to be to different, also the idea is to allow for any type of interface so not necessarily eth, could be anything. As long as ifconfig doesn't change, my little script will be able to pick up on the interface regardless of what it is.


All times are GMT -5. The time now is 05:00 PM.