2

I am running the command netsh wlan show interfaces from Python using subprocess.check_output and collecting it in a variable.

a = subprocess.check_output(["netsh","wlan","show","interfaces"])

And it is giving me output as:

>>> print a
>>> \r\nThere is 1 interface on the system: \r\n\r\n    Name                   : Wireless Network Connection\r\n    Description            : Ralink RT3290 802.11bgn Wi-Fi Adapter\r\n    GUID                   : 77cbe2d6-1a1e-41e7-be0a-3f18689c9ceb\r\n    Physical address       : c0:38:96:92:b7:1d\r\n    State                  : connected\r\n    SSID                   : PenguinWiFi\r\n    BSSID                  : 6c:72:20:d2:f9:21\r\n    Network type           : Infrastructure\r\n    Radio type             : 802.11n\r\n    Authentication         : WPA2-Personal\r\n    Cipher                 : CCMP\r\n    Connection mode        : Auto Connect\r\n    Channel                : 11\r\n    Receive rate (Mbps)    : 72\r\n    Transmit rate (Mbps)   : 72\r\n    Signal                 : 100% \r\n    Profile                : PenguinWiFi \r\n\r\n    Hosted network status  : Not available\r\n\r\n'

I want to get the status of wlan from this output as "connected" or "not connected".

For that I want to convert the above string into dictionary and want to use "state" as a key so that I can get the value from a key and decide whether wlan is connected or not connected.

But when I am converting it to a dictionary its giving error something like this:

>>> a_dict = dict(a)

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a_dict = dict(a)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

I am not getting what is the real problem, its with the string or something else?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Salil Tamboli
  • 108
  • 2
  • 12

2 Answers2

4

I am not getting what is the real problem, its with the string or something else?

You can't just convert a string to a dictionary, even if the string reads like one to a human.

You're going to have to parse that string and pluck the (key, value) pairs from it.

The key would be e.g. "Physical address" and the value "c0:38:96:92:b7:1d"

netsh has an XML output which is computer readable

The Netsh commands for the Windows Filtering Platform (WFP) enable you The tool then exports the collected data into an XML file that you can examine for clues to the cause of the problem.

Why not use the XML output instead of fragile hacks that may stop working at the tiniest console output format change?

netsh wlan export profile folder="PATH_TO_FOLDER" name=PROFILENAME

This ought to give you a computer readable file, easily traversable with Python numerous XML tools.

Forming dicts from a list of pairs

ValueError: dictionary update sequence element #0 has length 1; 2 is required

This is because dict expected a list of (key, value) pairs from which to form a dict. (key, value) has a length of 2 (it's a pair)

e.g.

>>> dict([('a', 1), ('b', 2)])
{'a': 1, 'b': 2}
Community
  • 1
  • 1
bakkal
  • 54,350
  • 12
  • 131
  • 107
1

Why don't simply use regex like:

>>> re.search('State\s+: (\w+)', a).group(1)
'connected'
>>> 

If it's needed to covert it to a dict, use re.split() with some like this:

>>> dict(map(str.strip, re.split('\s+:\s+', i)) 
         for i in a.strip().splitlines() if i.startswith('    '))

{'State': 'connected',
 'Hosted network status': 'Not available',
 'Network type': 'Infrastructure',
 'Physical address': 'c0:38:96:92:b7:1d', 
 'GUID': '77cbe2d6-1a1e-41e7-be0a-3f18689c9ceb',
 'Authentication': 'WPA2-Personal',
 'Description': 'Ralink RT3290 802.11bgn Wi-Fi Adapter',
 'Radio type': '802.11n',
 'Signal': '100%',
 'Connection mode': 'Auto Connect',
 'SSID': 'PenguinWiFi',
 'Channel': '11',
 'Profile': 'PenguinWiFi',
 'BSSID': '6c:72:20:d2:f9:21',
 'Name': 'Wireless Network Connection',
 'Cipher': 'CCMP', 'Receive rate (Mbps)': '72',
 'Transmit rate (Mbps)': '72'}


>>> dict(map(str.strip, re.split('\s+:\s+', i)) 
         for i in a.strip().splitlines() if i.startswith('    '))['State']

'connected'
Remi Guan
  • 21,506
  • 17
  • 64
  • 87