Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

MY_IPs uses hostname -I instead of hostname -i #8

Open
icebal opened this issue Aug 5, 2018 · 12 comments
Open

MY_IPs uses hostname -I instead of hostname -i #8

icebal opened this issue Aug 5, 2018 · 12 comments

Comments

@icebal
Copy link

icebal commented Aug 5, 2018

as the title says, pulling the list of IP addresses uses -i instead of -I. the command options under inetutils also supports -I has been removed.

@edylie
Copy link

edylie commented Aug 5, 2018

hostname -i returns

::1%1 127.0.0.1

if i used the other one hostname -I i get all my IPs.
172.16.16.130 192.168.0.1 192.168.0.252

172.16.16.130 (management)
192.168.0.1 (ctbd self ip)
192.168.0.252 (ctbd vip)

@icebal
Copy link
Author

icebal commented Aug 5, 2018

hostname -i returns 192.168.1.174 172.17.0.1 for me, inetutils ver. 1.9.4

this also reflects the documentation on https://www.gnu.org/software/inetutils/manual/inetutils.html#index-hostname

@edylie
Copy link

edylie commented Aug 5, 2018

i am running centos

[root@gluster-1683 ganesha]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@gluster-1683 ganesha]# rpm -qa | grep hostname
hostname-3.13-3.el7.x86_64

@icebal
Copy link
Author

icebal commented Aug 5, 2018

looks like this might be OS dependent, i'm running arch which is building from the latest release located here:
https://ftp.gnu.org/gnu/inetutils/inetutils-1.9.4.tar.xz

it looks like CentOS, Debian and Ubuntu use the Debian source, which is based off 1.9.3, but will soon be 1.9.4 and will have the change, unless they patch it back in. it might be more beneficial to do something like
MY_IPS="$(ip -4 -o addr show scope global | awk '{gsub(//.*/,"",$4); print $4}')"

context: i'm trying to package up storhaug for arch for private use, after testing will be moving it to AUR. since arch is always using latest, its a nice way to watch out for breaking changes in the future.

edit: another solution would be:
MY_IPS="$(hostname -I || hostname -i)"

@kalebskeithley
Copy link
Collaborator

FWIW, this usage of hostname -I came from CTDB and the hostname -I in the nfs-ganesha-callout file probably has the same problem.

In particular hostname -I works/worked fine on my single-homed RHEL/CentOS 7, Fedora 28, and Debian 10 boxes. It also works fine on my multi-homed Fedora 28. (multi-homed = hypervisor with bridged NAT)
But...
hostname -i returns an incorrect IP on the above Fedora 28 box (I'm not sure where it got it from) and useless IPv6 (for storhaug) on a different Fedora 28 box with IPv6 enabled.

And ip -4 addr show scope ... might work, but that bit of awk gives me a syntax error and an unterminated regexp error. I don't know awk so I can't suss out what's wrong with that gsub regex.

Back to the drawing board.

@kalebskeithley
Copy link
Collaborator

kalebskeithley commented Aug 7, 2018

Also FWIW, Fedora uses hostname from Debian
http://ftp.de.debian.org/debian/pool/main/h/hostname/hostname_3.20.tar.gz
not GNU inetutils.

Just looking quickly at gnu inetutils/src/hostname.c and debian hostname/hostname.c there doesn't seem to be much, if any relationship. But given that arch uses gnu inetutils it seems that the very least we need to do is icebal's MY_IPS="$(hostname -I || hostname -i)".

kalebskeithley added a commit that referenced this issue Aug 7, 2018
partial fix for #8

For distributions that use gnu inetutils/hostname which does not have the -I (capital letter i)
@dang
Copy link

dang commented Aug 7, 2018

Try this:

ip -4 --brief addr show scope global | grep UP | awk '{print $3}' | cut -d '/' -f 1

(note, ip addr show has an "up" parameter, but that doesn't seem to work for me; I still get down addresses listed)

@kalebskeithley
Copy link
Collaborator

RHEL/CentOS 7 don't know --brief. :-(

ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d '/' -f 1

seems to do the correct thing on all of my boxes. dang notes elsewhere that it will show addrs for DOWN nics, but I believe that hostname -I or hostname -i will show addrs for DOWN nics as well.

@kalebskeithley
Copy link
Collaborator

Any comments on the above? If I don't hear anything I will make that change to both files that are using hostname -I || hostname -i

@edylie
Copy link

edylie commented Aug 8, 2018

It appears the result is consistent on my end regardless which option

172.16.16.130 -> management IP
192.168.0.1 -> CTDB NODE IP
192.168.0.253 -> CTDB VIRTUAL IP

[root@gluster-1683 ~]# hostname -I || hostname -i
172.16.16.130 192.168.0.1 192.168.0.253
[root@gluster-1683 ~]# hostname -I
172.16.16.130 192.168.0.1 192.168.0.253
[root@gluster-1683 ~]# rpm -qa | grep hostname
hostname-3.13-3.el7.x86_64
[root@gluster-1683 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@gluster-1683 ~]#

so during setup, I need to modify both /usr/sbin/storhaug and /etc/ctdb/nfs-ganesha-callout to use this

hostname -I | awk '{print $2}'

Because I only want to build config based on my CTDB NODE IP

@icebal
Copy link
Author

icebal commented Aug 13, 2018

So far this has worked for me, other than a warning that -i doesnt exist before it tries -I

@kalebskeithley
Copy link
Collaborator

kalebskeithley commented Aug 15, 2018

Next idea (pseudo code)
ip route gives
default via 192.168.122.1 dev ens3 proto static metric 100
192.168.122.0/24 dev ens3 proto kernel scope link src 192.168.122.27 metric 100

then ip -4 addr -f inet | grep ens3 gives
ens3 UP 192.168.122.27/24

obviously ens3 could be eth0 or something else. Script accordingly

Or ip -4 -br addr and get/use the addr that's in /etc/ctdb/nodes

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants