Re: what's the right way to resolve localhost's IPs
On Wed, Mar 22, 2023 at 10:06:40AM +0800, Jeremy Ardley wrote:
>
> On 22/3/23 09:12, fh@dnsbed.com wrote:
> > Hello,
> >
> > In my shell script, how to get the localhost's IPs (eth0 and eth1)
> > correctly?
> > I know I can run 'ifconfig' and grep etc, but it's maybe not that
> > graceful.
>
> On Debian the preferred command is
>
> root@debian12:~# ip a
[...]
> You can also output in .json format
>
> root@debian12:~# ip -j a
[...]
>
> and if you write a script like this (and make it write protected and
> executable)
[...]
For those of us who like using jq for this kind of thing:
#!/bin/sh
ip -j a | jq -r '
.[] | {
"name": .ifname,
"addr": .addr_info |
map(select(.family == "inet")) |
.[0].local
} |
select(.addr) |
.name + ": " + .addr'
> you can do
>
> ip -j a | ./ipv4_addresses.py
> lo: 127.0.0.1
> enp7s0: 10.31.40.68
>
> or on a fancier setup
>
> ip -j a | ./ipv4_addresses.py
> lo: 127.0.0.1
> eth0: 10.31.40.4
> eth0: 10.31.40.5
> eth0: 10.31.40.101
> docker0: 172.17.0.1
>
> > --
>
> Jeremy
> (Lists)
--Gregory
Reply to: