1830880 Members
1924 Online
110017 Solutions
New Discussion

Quick scripting 10points

 
SOLVED
Go to solution
Stefan Saliba
Trusted Contributor

Quick scripting 10points

A quick one

I need to do "ifconfig lan0 | grep inet" and from the line I get I just get the IP address. i.e. pipe the output to something and get only the ip adress ??

ANY CLUES


13 REPLIES 13
Pete Randall
Outstanding Contributor
Solution

Re: Quick scripting 10points

ifconfig lan0 |grep inet |awk '{ print $6 }'

Pete

Pete
John Poff
Honored Contributor

Re: Quick scripting 10points

Hi,

For the quick and dirty way to get the IP address from something like this:



Try this:


# ifconfig lan0
lan0: flags=843
inet 10.100.12.23 netmask ffffff00 broadcast 10.100.12.255

# ifconfig lan0 | tail -1 | awk '{print $2}'

10.100.12.23


It may not work in all cases, but it is one way to do it.

JP

Dirk Wiedemann
Respected Contributor

Re: Quick scripting 10points

Hello Stefan,

try
ifconfig lan0 | grep inet | cut -s -d " " -f2

regards
Dirk
MANOJ SRIVASTAVA
Honored Contributor

Re: Quick scripting 10points

Hi


ifconfig lan0 | grep inet | awk '{print $2}'


will give you the IP adress only .


Manoj Srivastava
Jeff Schussele
Honored Contributor

Re: Quick scripting 10points

Hi Stefan,

do the following

ifconfig lan0 | grep inet | awk '{print $2}'

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Stefan Saliba
Trusted Contributor

Re: Quick scripting 10points

Thanks guys for the help

OK 10 points to all...


Good day to all
Stefan
CASE CLOSED
Pete Randall
Outstanding Contributor

Re: Quick scripting 10points

Aaaaakkkk! My mistake. Make that $2, not $6

Pete

Pete
Stefan Saliba
Trusted Contributor

Re: Quick scripting 10points

Thanks just the same Pete
BKUMAR
Frequent Advisor

Re: Quick scripting 10points

Hi

try this

ip1=`ifconfig lan0 | grep inet | awk '{print $2}'`

echo $ip1

will give the ip address stored in a variable

hth,
Unix Administration Most Dedicated and Challenging Career in the World
Tom Maloy
Respected Contributor

Re: Quick scripting 10points

One perl solution:

ifconfig lan0 | perl -n -e 'if (/inet ([\d.]*) netmask/) {print $1;}'

Tom
Carpe diem!
Ralph Grothe
Honored Contributor

Re: Quick scripting 10points

Couldn't resist, because I saw so many replys that suggested a piped combo of grep and awk.
Why on earth waste two processes with IPC involved where just one would suffice?

e.g.

# ifconfig lan0 | awk '$1~/inet/{print $2}'

Madness, thy name is system administration
Jeff Schussele
Honored Contributor

Re: Quick scripting 10points

Very good point Ralph.

Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
George A Bodnar
Trusted Contributor

Re: Quick scripting 10points

netstat -in | awk '{if ($1 == "lan0") print $4}'