概要
Python 3 で現在利用中の Wi_Fi の ESSID を取得する。
環境
- Raspberry Pi 3B
- Raspbian Strech
- Python 3.5.3
Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def get_ssid(): | |
cmd = 'iwconfig wlan0|grep ESSID' | |
r = subprocess.run(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)\ | |
.stdout.decode().rstrip() | |
idx = r.find('ESSID:') | |
return r[idx + 7:-1] | |
if __name__ == '__main__': | |
print(get_ssid()) |
コマンドラインで iwconfig | grep wlan0 を実行し、その結果を取り込み ESSID を切り出しているだけ。
subprocess.run().stdout の戻り値はバイト文字列なので、通常の文字列として扱うには decode() で変換する必要がある。
wlan0 は Raspbian の場合。他の OS の場合は iwconfig の結果を見て、適切に変更する。
コメント
[…] Python3 : 現在利用中の ESSID を取得する概要Python 3 で現在利用中の Wi_Fi の ESS… Raspberry Pi Rapsberry PiRaspbianstretchWi-Fiラズパイ シェアする Twitter Facebook […]