netlink icon indicating copy to clipboard operation
netlink copied to clipboard

Displaying if ip addr is assigned statically or via DHCP

Open ninjamarcus opened this issue 4 years ago • 2 comments

Using ip -j addr ls, converts the ip addr ls output into a json structure. We have been trying to get the same output with the netlink package but are stumbling at a few points. We are mostly interested in knowing if an ip address has been brought up by dhcp or assigned statically. The json output from ip commands shows - { "ifindex": 3, "ifname": "wlp0s20f3", "flags": [ "BROADCAST", "MULTICAST", "UP", "LOWER_UP" ], "mtu": 1500, "qdisc": "noqueue", "operstate": "UP", "group": "default", "txqlen": 1000, "link_type": "ether", "address": "4c:1d:96:7a:22:86", "broadcast": "ff:ff:ff:ff:ff:ff", "addr_info": [ { "family": "inet", "local": "192.168.65.90", "prefixlen": 18, "broadcast": "192.168.127.255", "scope": "global", "dynamic": true, "noprefixroute": true, "label": "wlp0s20f3", "valid_life_time": 6527, "preferred_life_time": 6527 },

The addr_info -> dynamic: true is the bit we are after. Given the ip command has it it must be available in netlink somewhere.

any suggestions would be welcomed.

ninjamarcus avatar Aug 19 '21 16:08 ninjamarcus

Have you tried view the IP command source code? I have found this, and the flag is described in rtnetlink doc as

ifa_flags is a flag word of IFA_F_SECONDARY for secondary address (old alias interface), IFA_F_PERMANENT for a permanent address set by the user and other undocumented flags.

Hope this can helpful to you.

laeo avatar Aug 29 '21 03:08 laeo

I processed like this (a.Flags&unix.IFA_F_PERMANENT != 0)

fuqiangZ avatar Jul 10 '23 06:07 fuqiangZ