2

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:

TIME (s)    SIGNAL STRENGTH (dBm)   BITRATE (Mbit/s)

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?

  • Do you want the script to overwrite the old data each time it starts? Or do you want it to append new data to the old data? – kos Mar 29 '16 at 16:26
  • The truth is that I would like to start the script was recorded with the start time (or ask for a name), not to overwrite the old data. – AntonioG Mar 29 '16 at 16:54
0

This should do:

#!/bin/bash
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
done

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 script.sh 
^C
% cat log 
2016-03-29T20:00:22+0200
TIME (s)    SIGNAL STRENGTH (dBm)   BITRATE (MBit/s)
0   63  54.0
5   40  54.0
10  63  54.0

To print also the RX bitrate:

#!/bin/bash
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
done
  • @AntonioG Strange, it works for me. Are you running the script in Bash? Try calling Bash explicitly: bash script.sh. – kos Mar 29 '16 at 18:14
  • Sorry, I was calling the script as sh script.sh. With bash script.sh me works very well Thanks soooooooo much for the help. – AntonioG Mar 29 '16 at 18:30
  • How can I edit the script to save tx bitrate and rx bitrate in log? . Now, only rx bitrate is saved. – AntonioG Mar 30 '16 at 6:33
  • @AntonioG See the update – kos Mar 30 '16 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.