ifconfig script

NightShade03NightShade03 Member Posts: 1,383 ■■■■■■■□□□
Alright I know there are a few of you out there that are much better with regex and awk/sed than I. I'm looking for the most efficient way in BASH to pull the IP address of the server (there is only one interface).

Here is what I have:

ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d ":" -f2 | awk '{print $1}'

I am sure there is probably a cleaner more efficient way to do this....anyone?

Comments

  • undomielundomiel Member Posts: 2,818
    Don't know if you'd call this cleaner or more efficient but you said you wanted a regex so here you go ... no calls to awk or sed either.


    ifconfig | egrep -m1 -o '([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|255[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|255[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|255[0-5])\.([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|255[0-5])' | head -n1
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • Forsaken_GAForsaken_GA Member Posts: 4,024
    assuming that the server's hostname is resolvable in DNS (and if it isn't, that's easy to fix in /etc/hosts), just use hostname -i, much simpler

    ifconfig eth0 | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | sed -n '1p'

    will probably do what you want as well

    And you could clean up your own command and get rid of a call to grep if you specify the interface in the first part instead of grabbing it all

    ifconfig eth0 | grep 'inet addr:' | cut -d ":" -f2 | awk '{print $1}'
  • NightShade03NightShade03 Member Posts: 1,383 ■■■■■■■□□□
    And you could clean up your own command and get rid of a call to grep if you specify the interface in the first part instead of grabbing it all

    ifconfig eth0 | grep 'inet addr:' | cut -d ":" -f2 | awk '{print $1}'

    :: Smacks self in head :: Good call. It's been a long 48 hours....clearly I'm not thinking straight. Thanks for the help guys.
  • lordylordy Member Posts: 632 ■■■■□□□□□□
    You could also have a look at the tools from the iproute2 package. I read somewhere (don't remember) that iproute is the way to go and ifconfig is kinda deprecated

    Example:
    /sbin/ip -o -4 addr list eth0

    The ip command has a lot of filter options so you might want to check the man-page icon_wink.gif
    Working on CCNP: [X] SWITCH --- [ ] ROUTE --- [ ] TSHOOT
    Goal for 2014: RHCA
    Goal for 2015: CCDP
  • Forsaken_GAForsaken_GA Member Posts: 4,024
    :: Smacks self in head :: Good call. It's been a long 48 hours....clearly I'm not thinking straight. Thanks for the help guys.

    Yeah, it really depends on whether you want to get everything, you might want to if you have aliases assigned, all depends on what this is for.

    If all you're doing is reporting it to a central place, I'd actually prefer to grab it via SNMP, though depending on what you're doing, that might seem a little circular since you have to know the IP to get it via SNMP hehe
  • NightShade03NightShade03 Member Posts: 1,383 ■■■■■■■□□□
    Actually using this to assign the IP address to a BASH variable to that I can generate a .conf file for a service.
  • Met44Met44 Member Posts: 194
    Since you say you're using bash, builtins will do for parsing:

    a=($(ip -o -4 addr show eth0)); builtin echo ${a[3]%/*}
    lordy wrote: »
    I read somewhere (don't remember) that iproute is the way to go and ifconfig is kinda deprecated

    The big difference between the two is that ifconfig uses the older ioctl method to talk to the kernel. Since ioctl is non-standard and a bit clunky, a new interface (the well documented, standard Netlink sockets) was introduced into Linux kernels 2.2+. The ip suite came along to provide access to all the new netlink stuff.

    ioctl is what gives you those charming alphabet-soup error messages like:

    > ifconfig eth0 down
    SIOCSIFFLAGS: Permission denied

    icon_smile.gif

    Netlink deprecated ioctl, so ifconfig is also deprecated since it uses ioctl.

    If you're not doing system programming, it's probably not a *huge* deal which you use, but there could be some far-far-future benefit in using 'ip'. Plus, the ip utility is useful for lots of things besides ip addressing (nat, ipsec, policy routing, etc), so it's worthwhile to be familiar with it anyway.

    By the way, thanks for posting the -o flag, didn't know there was such an option. Makes it much easier to sift through that output with the eye...
Sign In or Register to comment.