I need to collect all the interface names, even the ones that aren\'t up at the moment. Like ifconfig -a.
getifaddrs() is iterating through
I need the main device that is being used by the system (assuming there is only one) such as eth0 wlan0 or whatever RHEL7 is trying to do...
The best I hacked together was this:
#!/bin/bash
# -- Get me the interface for the main ip on system
for each in $(ls -1 /sys/class/net) ;do
result=$(ip addr show $each | awk '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}' | grep "$(hostname -I | cut -d' ' -f1)")
if [ ! -z "${result// }" ] && [ -d /sys/class/net/${each// } ] ;then
echo "Device: $each IP: $result"
break;
fi
done
Sample output:
./maineth.sh
Device: enp0s25 IP: 192.168.1.6