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
:
as a delimiter. Trim off the spaces around. -
Use
io.popen()
to grep the shell output. The use of:read('*a')
is necessary. It appends the trailing newline\n
character to the output. That’s fine for the functionconky_myspeed(upordown)
, butconky_myspeedgraph(upordown, h, w)
. Remove that with:gsub('\n$','')
. Theg
ingsub()
stands forglobal
. Thesub()
method requires two arguments limiting the scope of the string substitution. -
Since the Lua function is to be called from time to time by Conky, it’s a good idea to
:close()
the stream after having finished.
I tried putting everything in one single line without :close()
ing the stream.
A number 1
prepending with some white spaces were prepended into the output.
The rest of the Conky Lua script consist mainly of routine usage of
conky_parse()
. To avoid repetitions like ${lua myupspeed}
and ${lua mydownspeed}
, I’ve added one more argument in the function
conky_myspeed(upordown)
.
To end this post, I’m going to include a short and simple Perl-compatible regex pattern (PCRE) for matching the IP address in case that
curl http://ipecho.net/plain
returns an HTML document from an ISP that requires web portal login.
curl http://ipecho.net/plain | grep -Po '(\.?[0-9]{2,3}){4}' || echo 'Not found!'
That’s not a proper PCRE for IP extraction, since it matches .0.0.0.0
.
However, that’s easy to understand and short enough to be put inside the
.conkyrc
config file, so that git diff
’s output won’t be too heavy.