Get Local IP address using Sed, Awk

Hi All,

how to get solaris box local ip addresss in variable, using sed or awk utlities.

Thanks,
Mani Muthu

what is the output of the following command

/sbin/ifconfig -a

Hi i am getting the following ouput

lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
e1000g0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 10.100.19.98 netmask fffffe00 broadcast 10.110.9.255

i want to hold the '10.100.19.98' in a variable

try this...

ip_addresss=`/sbin/ifconfig -a | awk ' /inet.*broadcast/ "{print $2}"'`
1 Like
user@host> (/home/user) $ /sbin/ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 158.155.34.123 netmask ffff0000 broadcast 158.155.255.255
user@host> (/home/user) $ /sbin/ifconfig -a|awk '{if($1=="hme0:"){interface=1} if(interface==1 && $1=="inet"){print $2}}'
158.155.34.123
user@host> (/home/user) $

Here is the output of Solaris ifconfig and logic to get the IP of required interface, in my case hme0 is the interface

thanks @ itkamaraj
I am getting the output as few modification based on your code.

ifconfig -a | awk ' /(inet)(.*)broadcast/ {print $2}'

thanks @ kumaran_5555. But i don't under stand your logic and i am not able to get the output as per you mention method

 /sbin/ifconfig -a|awk '{if($1=="hme0:"){interface=1} if(interface==1 && $1=="inet"){print $2}}'

you have to change hme0 to e1000g0. The logic you have used may not work when you have more interface with broadcast enabled.

/sbin/ifconifg | sed '/ *inet/ ! d; /127\.0\.0\.1/d;s/ *inet \(.*\) n.*/\1/'