How to continuously monitor your Wi-Fi’s signal strength in Ubuntu
I was staying at an apartment last night that didn’t have its own Wi-Fi so I had to borrow from a neighbor. However, the signal was very weak and I had to find just the right spot in my apartment to get a decent connection.
Ubuntu has a utility called iwconfig that lets you view your wireless connection’s signal strength. Typing in‘iwconfig wlan0’ (where ‘wlan0’ is the interface name) in a shell would return something like this:
wlan0 IEEE 802.11abgn ESSID:”MySuperSecureNetwork” Mode:Managed Frequency:2.462 GHz Access Point: 00:11:22:33:80:9C Bit Rate=65 Mb/s Tx-Power=15 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off Link Quality=43/70 Signal level=-67 dBm Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:7496 Invalid misc:2033 Missed beacon:0
The main thing to look for here is the line that says ”Link Quality…”. In my case, I need a link quality of at least 32/70 to get a decent, reliable connection. Last night I was only getting between 28-31 and my connection was almost unusable and very unreliable. I figured I was very close and just needed to tweak the position of my laptop just a little bit more.
But it was getting annoying having to keep running the command as I change position. So I figured I’d write a Python script that would continuously run the command for me every second and parse the output to only display the line I care about, the one showing the link quality and signal level. I know I probably should’ve just written it in bash but I’m more familiar with Python and could do it very quickly.
I came up with something like this:
import subprocess import time import argparse parser = argparse.ArgumentParser(description='Display WLAN signal strength.') parser.add_argument(dest='interface', nargs='?', default='wlan0', help='wlan interface (default: wlan0)') args = parser.parse_args() print '\n---Press CTRL+Z or CTRL+C to stop.---\n' while True: cmd = subprocess.Popen('iwconfig %s' % args.interface, shell=True, stdout=subprocess.PIPE) for line in cmd.stdout: if 'Link Quality' in line: print line.lstrip(' '), elif 'Not-Associated' in line: print 'No signal' time.sleep(1)
By default, running the script will use interface wlan0. You can specify the interface name in the first positional argument (e.g. python wlansignal.py wlan1) in the command line.
The output of this script will look something like:
--Press CTRL+Z or CTRL+C to stop.-- Link Quality=41/70 Signal level=-69 dBm Link Quality=38/70 Signal level=-72 dBm Link Quality=38/70 Signal level=-72 dBm Link Quality=42/70 Signal level=-68 dBm Link Quality=41/70 Signal level=-69 dBm Link Quality=40/70 Signal level=-70 dBm Link Quality=41/70 Signal level=-69 dBm Link Quality=42/70 Signal level=-68 dBm ...
This script really helped me figure out just where to place my laptop as I can move around and look at the screen while the command runs. In my case, I figured out that I just needed to place my laptop about 3 inches higher from my table to get a link quality between 32-35, which was enough to get a reliable connection. So I just simply placed something underneath my laptop to raise it a few inches.