1

The man page of hostname tells us for the option -I: "Do not make any assumptions about the order of the output.".
Anyway, our servers always gave back the external IP in the first place and the local network IP in the second. Unfortunately, a lot of legacy code is running on this assumption whilst using hostname -I | cut -d' ' -f1 to get the external IP. Over the weekend, something changed. Now, the internal IP is displayed first and external second. This is a new situation after years and I wonder what could have influenced this change.
Any assumptions?

Operating System: Ubuntu 14.04.3 LTS
Kernel: Linux 4.4.110-x86_64-jb1

Daniello
  • 21
  • 1
  • 1
    There are way too many variables to even begin to guess why it changed. Ultimately it doesn't really matter what caused the change. Your developers were warned not to design the code this way, but they did anyway. All you can really do now is fix it, and this time make no assumptions about the ordering. You probably need to find the IP address with a different tool entirely. When I ran it just now I get 16 addresses back, 6 of them are global IP addresses, and they're all listed at the end. It's clearly not the right tool for whatever you are doing. – Michael Hampton Feb 04 '19 at 14:46
  • Potentially relevant: https://serverfault.com/q/690391/214507 – kasperd Feb 04 '19 at 15:20
  • The only files I can think of that could have any effect on the output of that command are `/etc/nsswitch.conf`, `/etc/hosts.conf` & '/etc/hosts`. – Marcel Feb 04 '19 at 17:14

1 Answers1

2

There are multiple ways of achieving the same result you need. Besides using /proc or "ip a" or "ss" there is a way to pull data using SALT or any other management tool.

I am not quite sure how "hostname" pulls up it's data, but I can at least give you another way to check what you need.

For ipv4 only:

ip -4 a show dev eth0  | grep inet | cut -d " " -f6 | cut -d "/" -f1

In case you have a secondary IP on the same interface and you need a primary:

ip -4 a show dev eth0:0  | grep inet | cut -d " " -f6 | cut -d "/" -f1

For ipv6 only:

ip -6 a show dev eth0  | grep inet | cut -d " " -f6 | cut -d "/" -f1

In case you will need a subnet mask you ignore the last "cut" command I used. You may want to replace eth0 with the actual interface you have, that information won't change at least.

Dmitriy Kupch
  • 471
  • 2
  • 6