When one changes connection type (say, from ethernet to Wi-Fi), the interface
name changes (e.g. eth0
→ wlan1
). To avoid changing Conky config file all
the time, here’s a little Lua function for finding the network interface name.
function findInterface()
local handle = io.popen('ip a | grep "state UP" | cut -d: -f2 | tr -d " "')
local result = handle:read('*a'):gsub('\n$','')
handle:close()
return result
end
-
ip a
gives everything about connection info. Each entry looks like3: wlp3s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
-
Grep the “state UP” and extract the second field, using
[Read More]:
as a delimiter. Trim off the spaces around.