LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2007, 02:38 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Rep: Reputation: 39
Question 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

Last edited by helptonewbie; 11-07-2007 at 03:54 PM.
 
Old 11-07-2007, 03:29 PM   #2
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Quote:
Originally Posted by helptonewbie View Post
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.
 
Old 11-07-2007, 03:52 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
i'm sorry but could you expand a little?
 
Old 11-08-2007, 06:21 AM   #4
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
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?
 
Old 11-08-2007, 06:52 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 11-08-2007, 08:10 AM   #6
lsteacke
Member
 
Registered: Jul 2007
Distribution: Ubuntu
Posts: 99

Rep: Reputation: 16
I would suggest just writing a more verbose script. Passing an argument instead of the ip is a great way to start.
 
Old 11-08-2007, 11:30 AM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
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.

Last edited by helptonewbie; 11-08-2007 at 11:37 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial Mouse is attached or not attached at seiral port lovelylinux Linux - Hardware 1 01-28-2007 07:21 PM
how to find physical address of kernel virtual address kushneeraj Programming 0 10-20-2006 07:29 PM
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
DHCP default route attached to loopback interface chiefdan Linux - Hardware 2 12-06-2005 05:08 PM
How to find IP address of a machine if I know their MAC Address dysenteryduke Linux - Networking 13 09-12-2005 10:21 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration