I'd like to implement a bash script that creates a mapping between the network cards (or interfaces) names in my computer and their IP address (in CIDR format, if possible).
The goal is having something like an associative array, or a file looking like:
lo: "127.0.0.1/8" eth0: "192.168.1.123/24" br-priv: "" # <-- No IP assigned wlan0: "10.42.0.10/24"
I have the
ifconfig and the
ip commands available, but the parsing of their outputs is a bit tricky, because the IP address and the interface's name don't appear in the same line so I would have to find the interface IP address and then rollback to find the network's name, or viceversa.
Is there any other way to find my interfaces IP addresses?
I have all the interfaces names (as directory names) under
root@borrajax:~# ls /sys/class/net/ br-priv/ eth0/ lo/ mon0/ wlan0/
But I haven't been able to find their IP in those directories. Doing that would be great, because I could just walk the directory, retrieve the directory's name and then read the IP from some other file.
This is intended to run in an embedded system running OpenWRT, but a solution for a Debian based system might be suitable. My
bash's version is 4.2.28, just in case.