Re: IP address variable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 14 Feb 2005 14:13:15 -0800, paul <paul.hamradio@xxxxxxxxxxx> wrote:
> >>  > > I need to create a variable (or read an existing one) that returns
> >> the IP > address for eth0.
> >>  > I used to do it like this under RH 7.2:
> >>  > > IPADDR='ifconfig eth0|grep inet|cut -d ":" -f2|cut -d " " -f1'

Or another way that's perhaps a bit more robust under Linux (it
handles secondary IP addresses, scoped sub-routing tables, etc)

   DEVICE=eth0

   IPADDR=$(/sbin/ip addr show dev $DEVICE scope global primary | \
     awk '$1=="inet" {print $2}' | \
     cut -d/ -f1)

use "inet6" if you want IPv6 addresses instead.

Also, I find it more readable to use the bash $(xxxx) shell syntax
rather than backticks `xxxxx`.  You can do that even for
you ifconfig solution.

-- 
Deron Meranda


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux