Hans also found the "pretty-wifi-info.lua" script for checking the Yún's WiFi status, it nicely abstracted Hans code from the hardware so he wrote a wrapper for that rather than writing his own. He used the "parse" module to interpret the output. That installed nicely on the Yún for a change.

 

pip install parse


 

To avoid key errors he defaulted all of the common values to be "unknown".

# Get the Wifi Status into a dictionary
import subprocess
from parse import *

def parselines(s):
    lines = s.splitlines()
    d = {}
    for line in lines:
        p = parse("{}: {}",line)
        if p:
            d[p[0]] = p[1]
    return d

def getWifiStatus():
    w = subprocess.check_output("/usr/bin/pretty-wifi-info.lua")
    p = parselines(w)
    for key in ('Mode','Interface name','SSID','Signal','Encryption method','Active for','IP address','MAC address','RX/TX'):
        p.setdefault(key,'Unknown')
    return p

p = getWifiStatus()

print p['Mode']
print p['Interface name']
print p['SSID']
print p['Signal']
print p['Encryption method']
print p['Active for']
print p['IP address']
print p['MAC address']
print p['RX/TX']

 

Reference

 

https://docs.python.org/2/library/subprocess.html#using-the-subprocess-module

https://pypi.python.org/pypi/parse

https://wiki.python.org/moin/KeyError