LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2011, 04:30 PM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
how to grep IPs from ifconfig output


Following is my ifconfig output

Code:
    eth0      Link encap:Ethernet  
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
              Interrupt:28 Base address:0x2000 
    
    eth1      Link encap:Ethernet  
              inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
    	  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:36497 errors:0 dropped:0 overruns:0 frame:14515
              TX packets:44884 errors:1352 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:20781745 (20.7 MB)  TX bytes:17776225 (17.7 MB)
              Interrupt:17 Base address:0xc000 
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:12 errors:0 dropped:0 overruns:0 frame:0
              TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:720 (720.0 B)  TX bytes:720 (720.0 B)
    
    virbr0    Link encap:Ethernet  
              inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
    
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 B)  TX bytes:4416 (4.4 KB)
    
    vmnet1    Link encap:Ethernet 
              inet addr:192.168.185.1  Bcast:192.168.185.255  Mask:255.255.255.0
    
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    vmnet8    Link encap:Ethernet  
              inet addr:192.168.207.1  Bcast:192.168.207.255  Mask:255.255.255.0
    
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
I want to do some thing grep that I see the IP corresponding to each LAN card?
Is that possible?
How can it be achieved?
 
Old 03-06-2011, 04:42 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Something like this?

Code:
ifconfig -a      | \
grep -A1 "Link " | \
grep -v -- '--'  | \
paste - -        | \
awk '{if ($4 ~/inet/) print $1,$5;if ($5 ~/inet/) print $1,$6;if ($6 ~/inet/) print $1,$7;}'

Last edited by macemoneta; 03-06-2011 at 04:46 PM. Reason: Corrected for additional cases
 
1 members found this post helpful.
Old 03-06-2011, 07:13 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am not sure why we have to make it so complicated?
Code:
ifconfig | awk '/Link/{printf $1}/inet/{print gensub(/addr:/," ","1",$2)}'
 
1 members found this post helpful.
Old 03-06-2011, 07:17 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by grail View Post
I am not sure why we have to make it so complicated?
Code:
ifconfig | awk '/Link/{printf $1}/inet/{print gensub(/addr:/," ","1",$2)}'
That gives me (on the sample data):
Code:
eth0eth1 192.168.1.2
lo 127.0.0.1
 
virbr0 192.168.122.1
vmnet1 192.168.185.1
vmnet8 192.168.207.1
Mine gives:
Code:
eth1 addr:192.168.1.2
lo addr:127.0.0.1
virbr0 addr:192.168.122.1
vmnet1 addr:192.168.185.1
vmnet8 addr:192.168.207.1

Last edited by macemoneta; 03-06-2011 at 07:20 PM.
 
1 members found this post helpful.
Old 03-06-2011, 09:11 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Fair call, forgot not all might have an address. Easy fix:
Code:
ifconfig | awk '/Link/{name = $1}/inet/{print name,gensub(/addr:/," ","1",$2)}'
 
1 members found this post helpful.
Old 03-06-2011, 09:28 PM   #6
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by grail View Post
Fair call, forgot not all might have an address. Easy fix:
Code:
ifconfig | awk '/Link/{name = $1}/inet/{print name,gensub(/addr:/," ","1",$2)}'
You need a space after the inet search string, otherwise you'll pick up IPv6 addresses (inet6, as on the loopback port).
 
1 members found this post helpful.
Old 03-06-2011, 09:40 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well we need to leave something for the OP to do ... lol
 
Old 03-06-2011, 11:24 PM   #8
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Great
 
Old 03-06-2011, 11:57 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
My version using sed. I've recently been working on how to do multi-line stuff with it.
Code:
ifconfig | sed -rn '/Link/ { N ; s/^([^ ]+).*inet addr:([^ ]+).*/\1 \2/p }'
I'm assuming that the inet addr is always found on the second line. But you can add more N commands to it if you need to slurp more lines in.
 
1 members found this post helpful.
  


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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-31-2009 12:21 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 11:22 AM
ifconfig/netconfig in Redhat 9 (static IPs) GomiCowboy Linux - Networking 6 09-07-2003 02:11 PM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration