0

ipconfig does not exist anymore as a command available in Ubuntu 20.04 and later I assume. The new command is just ip. When I run the ip address command I get the entire list of all devices and ip addresses associated. I want just the eth0 device and public ip 4 address associated.

I want just the bare ip address octets only. I want this to work on both Linux and Mac OS.

james-see
  • 12,210
  • 6
  • 40
  • 47
  • Does this answer your question? [How to get the primary IP address of the local machine on Linux and OS X?](https://stackoverflow.com/questions/13322485/how-to-get-the-primary-ip-address-of-the-local-machine-on-linux-and-os-x) – miken32 Aug 19 '22 at 22:08
  • That question is closed @miken32 :) – james-see Aug 20 '22 at 23:05
  • That's because it isn't a programming question. It's still a duplicate – miken32 Aug 20 '22 at 23:16

1 Answers1

0

I found this pipe of cut and sed to work fine to get what I want:

on linux:

ip a | grep eth0 | cut -d " " --fields=6 | sed '2q;d' | awk -F'/' '{print $1}'

on BSD / Darwin / Mac OS:

ip a | grep en0 | tr -s 'inet' ' ' | sed '2q;d' | tr -s '' | awk -F' *? *' '{print $2}' | awk -F '/' '{print $1}'

which results in just the bare ip address I needed. I had to do some trial and error on what field column I actual needed. This probably could be more generalized, but this works for my use case.

Added a public git to just curl and run from anywhere like:

curl -L https://cutt.ly/UUYcT1r | /bin/bash
james-see
  • 12,210
  • 6
  • 40
  • 47
  • worth noting that sometimes the device is not `eth0` but something like `enp3s0f0` so the script will need to be adjusted for local server as needed – james-see Jan 10 '22 at 05:26
  • 1
    `ip -4 -o address show dev eth0 | awk '{print $4}'` is more succinct on Linux. Awk is designed to work with delimited data and do pattern matching, so piping to `cut` and `grep` is never necessary. But in this case you can just tell `ip` to do a one-line output. Note macOS doesn't have `ip` installed. – miken32 Aug 19 '22 at 22:09
  • I see. So I did not know about the show dev parameters in addition to `ip address`. Thanks for the info. – james-see Aug 20 '22 at 23:06