Operating System - HP-UX
1833178 Members
3168 Online
110051 Solutions
New Discussion

Re: Script to extract interface name and IP from ifconfig

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

Script to extract interface name and IP from ifconfig

I redirected the iconfig o/p to a file which contains,

lan0: flags=843
inet 19.79.191.17 netmask ffffff00 broadcast 19.79.191.255

lan0:1: flags=843
inet 19.79.191.18 netmask ffffff00 broadcast 19.79.191.255

lan0:2: flags=843
inet 19.79.191.20 netmask ffffff00 broadcast 19.79.191.255

now I need a script to extract only the interface name and the IP. The o/p should be something like this,

lan0 19.79.191.17
lan0:1 19.79.191.18
lan0:2 19.79.191.20

Pl. help.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
7 REPLIES 7
Dani Seely
Valued Contributor

Re: Script to extract interface name and IP from ifconfig

Run ifconfig and use the "cut" command to remove the desired sections from the output. I'd give you the syntax but my machine won't boot right now so I can't work at it, but give it a shot.
Together We Stand!
Karthik S S
Honored Contributor

Re: Script to extract interface name and IP from ifconfig

yeah I tried with cut and awk but having some difficuties due to the non uniform field separators in the ifconfig output. I thought someone at ITRC would have already tried this so posted here (in other words I am too lazy to write the script now :-))

Thanks anyway

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Scott Van Kalken
Esteemed Contributor

Re: Script to extract interface name and IP from ifconfig

lanscan | awk '{print $5}' | grep -v Net | grep -v Name | xargs -l ifconfig
Karthik S S
Honored Contributor

Re: Script to extract interface name and IP from ifconfig

Hi Scott,

That doesn't seems to be working for me. I prefer a script that can do the task using the input file as I specified above.

Thanks,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Scott Van Kalken
Esteemed Contributor
Solution

Re: Script to extract interface name and IP from ifconfig

#!/bin/sh
LAN=$(lanscan | awk '{print $5}' | grep -v Net | grep -v Name)

for card in $LAN
do
CONFIG=$(ifconfig $card)
echo $CONFIG | awk '{print $1"\t"$4}'
done

~
Scott Van Kalken
Esteemed Contributor

Re: Script to extract interface name and IP from ifconfig

I should mention that this will fail with a:

ifconfig: no such interface

if you have a physical card that shows up under lanscan but is not configured.

Sorry, I misunderstood your original post.
V.Tamilvanan
Honored Contributor

Re: Script to extract interface name and IP from ifconfig

Hi,
The below script works for me.Give the input file as argument to the script.

typeset -i z
x=`cat $1|awk '/lan/{print $1}'`
y=`cat $1|awk '/inet/{print $2}'`
z=1
for i in $x
do
a=`echo $y|cut -d " " -f$z`
i=${i%?}
echo $i "\t" $a
z=z+1
done