[Cialug] Just Tell Me The IP!

David Champion dchamp1337 at gmail.com
Thu May 21 17:28:37 UTC 2020


I did this ugly code in python for a rpi project - it displays the IP
address on a 2x LCD screen on the pi when it boots.

#!/usr/bin/python

from time import sleep
from datetime import datetime
import netifaces as ni

aAddr = ni.interfaces()
nAddr = 0

# populate array first
aInts = []

while nAddr < len(aAddr):
  nAddr = nAddr + 1
  if nAddr > len(aAddr) - 1:
    break
  try:
    x =  ni.ifaddresses(aAddr[nAddr])[2][0]['addr'] + ":" + aAddr[nAddr]
    aInts.append(x)
  except:
    sleep(0)

nAddr = 0

print(aInts[0])

On Thu, May 21, 2020 at 12:26 PM Dave Weis <djweis at sjdjweis.com> wrote:

> 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
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>


More information about the Cialug mailing list