Whenever you have a new WiFi device to install, the first step is to configure the connection settings. And often, the most time-consuming part is finding and typing in the password for the WiFi access point. We've enhanced the tool we introduced a few weeks ago to automate this step.
The aim is to add a button that retrieves the WiFi settings (SSID and password) used by the computer and then applies them to the YoctoHub-Wireless-n. Obviously, this only works if the computer has a network card and is currently connected to the WiFi network, but this is probably a majority of cases.
So we've added a "Use computer settings" button that selects the network used by the computer and automatically fills in the password. Just press "connect" and the YoctoHub-Wireless-n is automatically connected to the same network.
The new button Use computer settings
If the computer has no WiFi network card, this button is disabled. Note that you need to run the tool with administrator privileges for it to retrieve WiFi passwords.
Obtaining the WiFi settings
The method for obtaining WiFi settings is different for the three OSes (Windows, macOS, and Linux), but in all cases the application must have administrator privileges.
On Windows
Under Windows, the solution is to use netsh.
The "netsh wlan show profile" command provides a list of registered WiFi networks. We only need to scroll through the list to find the network currently in use.
try:
result = subprocess.run(
["netsh", "wlan", "show", "profile"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True
)
lines = result.stdout.strip().split("\n")
for line in lines:
if "All User Profile" in line:
return line.split(":")[1].strip()
except subprocess.CalledProcessError:
return None
return None
To obtain the password for a network, we use the "netsh wlan show profile" command, adding the "key=clear" option to display the password in clear text.
result = subprocess.run(
["netsh", "wlan", "show", "profile", f "name={ssid}", "key=clear"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True
)
for line in result.stdout.splitlines():
if "Key Content" in line:
return line.split(":")[1].strip()
return None
Under macOS
Under macOS, we use the "networksetup -getairportnetwork" command to obtain the SSID of the network used by the Mac.
result = subprocess.run(
["networksetup", "-getairportnetwork", "en0"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True
)
output = result.stdout.strip()
if "Current WiFi Network" in output:
ssid = output.split(": ")[1]
return ssid
return None
For the password, we use the "security find-generic-password" command, which retrieves passwords registered by the OS. Unlike Windows, this command displays a pop-up asking the user to authenticate themselves before displaying the password.
macOS checks authentication
try:
result = subprocess.run(
["security", "find-generic-password", "-D", "AirPort network password",
"-a", ssid, "-w"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True
)
password = result.stdout.strip()
return password
except subprocess.CalledProcessError as e:
return None
Under Linux
Under Linux, as is often the case, things are a little more complicated. Depending on the distribution and kernel version, there are several ways to retrieve the SSID of the current network. The most common way is to use nmcli.
result = subprocess.run(
["nmcli", "-t", "-f", "ACTIVE,SSID", "dev", "wifi"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True
)
lines = result.stdout.strip().split("\n")
for line in lines:
if line.startswith("yes:"):
return line.split(":")[1]
return None
It is not possible to obtain the password in clear text under Linux. Linux doesn't store the password, only a hash of the password.
Conclusion
We also took the opportunity to slightly improve our tool's interface by adding a scrollbar for the list of detected networks. This little tool is now a little easier to use, and you have a reference implementation if you want to add this functionality to your application.
As usual, the source code is available on GitHub: https://github.com/yoctopuce-examples/wifi-configurator