Created
August 30, 2020 03:41
-
-
Save tkoki/916f66a218ce45520f2bc40f3c2db466 to your computer and use it in GitHub Desktop.
Raspberry Pi OS でWi-Fiが有効かを確認する
This file contains 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
# coding: UTF-8 | |
import subprocess | |
import re | |
def extract_essid(line): | |
# ESSID:"essid" のパターンがあるかどうか | |
pattern = r'.*ESSID:"(.*)"' | |
result = re.match(pattern, line) | |
if result: | |
return (True, result.group(1)) | |
else: | |
return (False, 'No ESSID') | |
def main(): | |
ps = subprocess.Popen(['iwconfig'], stdout = subprocess.PIPE, stderr = subprocess.STDOUT) | |
try: | |
output = subprocess.check_output(('grep', 'ESSID'), stdin = ps.stdout) | |
result = extract_essid(output.decode()) | |
if result[0]: | |
print('OK ' + result[1]) | |
else: | |
print('NG ' + result[1]) | |
except subprocess.CalledProcessError: | |
print('No Wi-Fi connected.') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment