Networking – Simple Script for Monitoring Wireless Status


I want to create a script that monitors the status of the network (I do not know if there are any programs that already do what I want to do even better).

I'm only interested in monitoring the signal strength and the bitrate of a device connected to an access point, and I'm trying to create a .sh script to do that, for example for a minute, and to create an output file, for example, with this format:


00                   -53                 54

05                   -50                300

10                   -55                 54

15                   -60                 36

This data can be obtained from the command:

$ iw dev wlan1 station dump
Station 12:34:56:78:9a:bc (on wlan0)
        inactive time:  304 ms
        rx bytes:       18816
        rx packets:     75
        tx bytes:       5386
        tx packets:     21
        signal:         -29 dBm
        tx bitrate:     54.0 MBit/s

Or the command:

$ iw dev wlan0 link
Connected to 04:21:b0:e8:c8:8b (on wlan0)
        SSID: attwifi
        freq: 2437
        RX: 2272 bytes (18 packets)
        TX: 232 bytes (3 packets)
        signal: -57 dBm
        tx bitrate: 36.0 MBit/s

I do not know if I have explained this well, I want to get this data in any way and save it in a file (e.g. .txt). I want to periodically write the data I need (Tx bitrate and signal) to a file, therefore the file will get bigger over time.

The way to get the data I do not know what would be, I know this data appears in the commands that I put above.

I would like for the script to record the start time (or to ask for a name) without overwriting the old data.

Could anyone help me in creating a script to get what I want?
And another thing, how could I create the timer?
Or failing that, could someone tell me some tool to do something?
Does someone know a tool to do what I want to do?

Best Answer

This should do:

printf '%s\nTIME (s)\tSIGNAL STRENGTH (dBm)\tBITRATE (MBit/s)\n' "$(date --iso-8601=seconds)" >>log
for ((i=0; i<=60; i=i+5)); do
    iw dev wlp3s0f0 station dump | awk -vt=$i '$1=="signal:"{s=$2} $2=="bitrate:"{b=$3} END {printf "%d\t%d\t%.1f\n", t, s, b}' >>log
    sleep 5

It will append the current time in ISO 8601 format followed by that header to a file named log in the current working directory and append the relevant output of iw dev wlan1 station dump in that format to it every 5 seconds, for 60 seconds.

Sample run on my machine:

% bash 
% cat log 
0   63  54.0
5   40  54.0
10  63  54.0

To print also the RX bitrate:

printf '%s\nTIME (s)\tSIGNAL STRENGTH (dBm)\tTX BITRATE (MBit/s)\tRX BITRATE (MBit/s)\n' "$(date --iso-8601=seconds)" >>log
for ((i=0; i<=60; i=i+5)); do
    iw dev wlp3s0f0 station dump | awk -vt=$i '$1=="signal:"{s=$2} $1="tx"&&$2=="bitrate:"{tb=$3} $1="rx"&&$2=="bitrate:"{rb=$3} END {printf "%d\t%d\t%.1f\t%.1f\n", t, s, tb, rb}' >>log
    sleep 5