[Cialug] Just Tell Me The IP!

Dave Weis djweis at sjdjweis.com
Thu May 21 17:25:40 UTC 2020


You will need to track down ip and execute that. Save the output. Use
netstat-rn to find the routing table and correlate the two.


On Thu, May 21, 2020, 12:19 PM Scott Yates <Scott at yatesframe.com> wrote:

> Darnit, that won't work either, sorry.  Interesting problem though.
>
> On Thu, May 21, 2020 at 12:13 PM Scott Yates <Scott at yatesframe.com> wrote:
>
> > You might try the output of "route".  I think it is more universal, and
> > might be easier to parse out.
> >
> > On Thu, May 21, 2020 at 12:11 PM Todd Walton <tdwalton at gmail.com> wrote:
> >
> >> I'm running a mix of CentOS, Amazon Linux, and Ubuntu. The CentOS
> servers
> >> span three generations, the Amazon Linux, like, four. And you know
> what's
> >> surprisingly complicated to do across a variety of distributions and
> >> versions? Tell me the IP of the server I'm on. Well, if I'm "on" the
> >> server, it's just a couple of quick commands and an eyeball. But I need
> a
> >> way to do it by script.
> >>
> >> Problems include:
> >>
> >> * The "ip" command shows up in three different locations, depending on
> >> distribution.
> >> * The "ifconfig" command shows up in 2 different locations.
> >> * You can run those just fine sans sudo, but sbin is typically not in a
> >> user's PATH.
> >> * Network device names vary across distributions.
> >> * Some servers have multiple IPs. I need one, it needs to be from a
> >> "physical" device, and it needs to be the same one every time.
> >> * ip's output varies across versions
> >> * ip has json output, but only for a couple years now, so it's not in
> any
> >> of my distros </grouse>
> >>
> >> Is there just no reliable way to get a server's "primary" IP that works
> >> across distros and distro versions? I wish there was something like
> >> "hostname" that just spat back /the IP address/.
> >>
> >> This is what I'm doing right now:
> >>
> >> HOSTIP=$(find /sys/devices -type d -name 'net' -not -path '*/virtual/*'
> >> -print | xargs -I% find % -mindepth 1 -maxdepth 1 -exec basename {} + |
> >> grep -P '^e' | xargs -n1 ifconfig | grep 'inet ' | tr -s ' ' | sed -e
> >> 's/^[[:space:]]\+//g' -e 's/addr://g' | cut -d\  -f2 | sort | head -n1)
> >> if [[ -z $HOSTIP ]]; then HOSTIP='$(ifconfig | grep 'inet ' | tr -s ' '
> |
> >> sed -e 's/^[[:space:]]\+//g' -e 's/addr://g' | cut -d\  -f2 | sort |
> head
> >> -n1)'; fi
> >> if [[ -z $HOSTIP ]]; then HOSTIP='127.0.0.1'; fi
> >>
> >> It's fraught with disappointment, but less than anything else I've tried
> >> so
> >> far.
> >>
> >> (Also, I didn't realize until this little adventure that xargs will
> always
> >> run once, even without input, unless you explicitly tell it not to.
> Ugh.)
> >>
> >> --
> >> Todd
> >> _______________________________________________
> >> Cialug mailing list
> >> Cialug at cialug.org
> >> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
> >>
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>


More information about the Cialug mailing list