parsing ifconfig output

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing ifconfig output
# 1  
Old 05-09-2011
parsing ifconfig output

I'm trying to gather information on the interfaces on a large number of servers.
If I run ifconfig I will get:
eth0 Link encap:Ethernet HWaddr 00:50:56:A2:27:C1
inet Bcast: Mask:
eth1 Link encap:Ethernet HWaddr 00:50:56:A2:1D:21
inet Bcast: Mask:
I have tried :
/sbin/ifconfig -a | egrep "inet|Link" | awk '{ if ( $2 == "Link" ) {print  $1} else { if( $1 == "inet" )  { print $2 }}}'

Which gives me:
What I want is this:
Some interfaces do not have addresses so I cant just concatenate sequential lines. Is there a simple way for me to get the format I'm looking for ?


Last edited by Franklin52; 05-11-2011 at 04:39 AM.. Reason: Please use code tags
# 2  
Old 05-09-2011
Try this. This will give the interfaces which had ip address assigned.

ifconfig -a | awk '$2~/^Link/{_1=$1;getline;if($2~/^addr/){print _1" "$2}}'

I am using Linux/Fedora. If you are using Solaris, output of ifconfig might differ.


Last edited by ahamed101; 05-10-2011 at 12:51 PM..
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 05-10-2011
Nice ! I have it working for all my Solaris and Linux servers now
if [ `uname | awk '{print $1}'` == "Linux" ]; then /sbin/ifconfig -a | awk '$2~/^Link/{_1=$1;getline;if($2~/^addr/){print _1" "$2}}'; else ifconfig -a | awk '$2~/^flags/{_1=$1;getline;if($1~/^inet/){print _1" "$2}}';fi

Last edited by C0ppert0p; 05-10-2011 at 11:33 PM.. Reason: adding code example
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Assigning ipv6 to bonding interface - getting old as well as changed ipv6 in ifconfig output

Hi, I have created a bonding bond1 interface with 6 Eth , mode=4. Recently i have changed my old ipv6 to new one and tried to restart as well as reload network service. Post which i can see old as well as changed ipv6 in ifconfig command output. Below are few files and command output for your... (1 Reply)
Discussion started by: omkar.jadhav
1 Replies

2. Shell Programming and Scripting

Parsing nsupdate's output

Anybody that's ever used nsupdate knows that it's error management is not very good. I have a wrapper script that when it's got all the information it needs launches the nsupdate command. This is my attempt at parsing the output to help support users quickly know if the command succeded or... (7 Replies)
Discussion started by: maverick72
7 Replies

3. Shell Programming and Scripting

Grabbing IP and zonename from multiline 'ifconfig' output

Hi There, I have a Solaris server that has a bunch of zones configured and I am trying to write a script that will take all interfaces other than the loopback ones (e.g. lo0:3 etc) and present them so that I can easily determine the zone that owns the IP So in the case of the following... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

4. Shell Programming and Scripting

Parsing Output of a Variable

i have a log file that contains something similar to this: one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven twelve... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

parsing output

Can somebody provide a solution to parse the following; cat /tmp/xxx Name: QUE_REQU (o.mtaseast-o.dmart) (MTPost queue) Number of messages: 66446 (Age 686 min; Size 214 mb) Backlog (messages): 0 (Age 0 min) Name: QUE_REQU... (6 Replies)
Discussion started by: BeefStu
6 Replies

6. Shell Programming and Scripting

Parsing the date output

Hi fellows, I need to define a notification for SSL certificate expiration. My Command output is below: (this is the "Expiration Date") Tue Mar 15 09:30:01 2012 So, at 15th Feb (1 month before the expiration), a notification has to be triggered by a script or sth else. How can i set an... (5 Replies)
Discussion started by: oduth
5 Replies

7. OS X (Apple)

Ifconfig output - help understanding flags 'Smart, Simplex', etc

Hi - Trying to understand a few things from an ifconfig -a output - can't seem to find info anywhere on the net. Specifically - looking to understand the following: Flags=8863 Smart Running (is this the same as UP) Simplex inet6 supported media: autoselect - does that imply the... (1 Reply)
Discussion started by: littlefrog
1 Replies

8. Shell Programming and Scripting

Parsing output

I need to parse the following out put and determine if the USB is a DISK and whether or not it's External. If an HBA line contains "USB" then does the next line contain "DISK" and "External". 0:0,31,0: HBA : (aacraid,1) AAC SCSI 0,0,0: DISK : Adaptec ASR4800SAS Volu0001 ... (6 Replies)
Discussion started by: lochraven
6 Replies

9. Shell Programming and Scripting

parsing output

I have a file that contains the output of the ls -iR command, something like this: ./results: 2504641011 result_1410 2500957642 result_525 2504641012 result_1425 2500957643 result_540 ./tests/1: 2500788755 1 2500788743 1000 ./tests/2: 2500788759 3 2500788758 999 ... (6 Replies)
Discussion started by: looza
6 Replies

10. UNIX for Dummies Questions & Answers

Further question on 'ifconfig' output

I asked a similar question earlier and got a very good answer but a new doubt came up. This is a few lines of a '/sbin/ifconfig' command on my PC: RX packets:3781025 errors:0 dropped:0 overruns:0 frame:0 TX packets:1941909 errors:0 dropped:0 overruns:0 carrier:0 Does the RX and TX packets... (1 Reply)
Discussion started by: mint1981
1 Replies
Login or Register to Ask a Question

Featured Tech Videos