Re: [dev] ipaddr - script friendly ifconfig/ip replacement

From: Jeremy <jer_AT_jer.cx>
Date: Sun, 26 Mar 2023 10:54:19 -0700

This is the output on my machine:
10.0.1.176 (enp0s25)
10.0.1.176 (enp0s25)
10.0.0.3 (wg0)

Notice the duplicate addresses for enp0s25.
As I'm sure you know, as per the IP spec, a NIC can have multiple addresses,
so long as they're on different subnets.

Output of `ip -br address show enp0s25`:
enp0s25 UP 10.0.1.176/24 metric 50 172.20.1.1/24

The underlying issue is that you call getifaddrs.3, which gives you a list of
addresses(unique), and their corresponding NIC names(not necessarily unique),
iterating over that & calling SIOCSIFADDR by ifname.

One alternative is to just get the address from ifaddrs->ifa_addr.

Jeremy

On 03/26/23 01:08PM, Sean MacLennan wrote:
> I started writing ipaddr almost 20 years ago after the umpteenth
> time trying to parse ifconfig (and later ip) output in scripts. The
> original version was get only and Linux only.
>
> It now supports Linux, QNX, and most BSDs. However, I rarely use BSD
> these days... so the set functions may not work 100%.
>
> I thought there might be somebody in the suckless community that finds
> it helpful.
>
> You need just the ip address of eth0?
> % ipaddr eth0
> 192.168.1.173
>
> Nice simple output. All error output goes to stderr, so you can get
> just the IP or nothing. No awk/sed/cut needed.
>
> Also need the bitmask?
> % ipaddr -b eth0
> 192.168.1.173/24
>
> Maybe the netmask instead?
> % ipaddr -im eth0
> 192.168.1.173 255.255.255.0
>
> It can also be used to setup an interface:
> % ipaddr eth0 192.168.0.33/24 192.168.0.1

> Will set the ip, netmask and default gateway. I could have also
> specified the netmask the old way: 255.255.255.0.
>
> You can see ipaddr.c on github:
> https://github.com/smaclennan/samtools/blob/master/ipaddr.c
>
> I can email the file to anybody who wants it. It is only 6k gzipped.
>
> Cheers,
> Sean
>
Received on Sun Mar 26 2023 - 19:54:19 CEST

This archive was generated by hypermail 2.3.0 : Sun Mar 26 2023 - 20:00:09 CEST