How can I monitor the wireless signal level and frequency and log it in csv format?
When running iw help
you see a warning:
Do not screenscrape this tool, we don't consider its output stable.
There are two sections below: First a solution to your problem avoiding iw
and second an answer to your question. Both work with traditional awk
as well as (the default) GNU Awk
.
Solution to your problem
I'd use iwconfig
instead of iw
:
while sleep 1; do
iwconfig wlan0 | awk -F'[ :=]+' '/Freq/{gsub("\\.","");f=$5}/Signal/{s=$7}END{print s","f}'
done >>log.csv
Output
$ iwconfig wlan0 | awk -F'[ :=]+' '/Freq/{gsub("\\.","");f=$5}/Signal/{s=$7}END{print s","f}'
-44,2412
Notes
I got confused whether you want the values separated by ,
or rather a tab (which would be \t
), I chose ,
without and surrounding spaces here. If that's not what you wanted just change s","f
accordingly, s
is the Signal
and f
the Frequency
value there.
I also moved the redirection, this way the file doesn't have to be opened on each run but just once.
Explanations of the awk
part
-
-F'[ :=]+'
– sets a different field delimiter, here to one or more (+
) of the three characters enclosed in square brackets -
/Freq/{gsub("\\.","");f=$5}
– in the line(s) with “Freq”, replace every dot by nothing (because the frequency in theiwconfig
output uses a dot as the thousands separator) and save the content of the fifth column in variablef
-
/Signal/{s=$7}
– in the line(s) with “Signal”, save the content of the seventh column in variables
-
END{print s","f}
– after processing the input,print
the variabless
andf
with a literal comma between them
Answer to your question
while sleep 1; do
iw dev wlan0 link | awk '/freq/{f=$2};/signal/{s=$2}END{print s","f}'
done >>log.csv
Output
$ iw dev wlan0 link | awk '/freq/{f=$2};/signal/{s=$2}END{print s","f}'
-43,2412
Explanations of the awk
part
-
/freq/{f=$2}
– in the line(s) withfreq
, save the second column (space separated) in variablef
-
/signal/{s=$2}
– in the line(s) withsignal
, save the second column in variables
-
END{print s","f}
– after processing the input,print
the variabless
andf
with a literal comma between them