Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Source: https://stackoverflow.com/questions/60422924/writing-an-external-program-to-interface-with-wpa-supplicant
- Commands: https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html
- """
- import json
- import os
- import time
- from contextlib import contextmanager
- from socket import AF_UNIX, SOCK_DGRAM, socket
- @contextmanager
- def connect():
- req_sock = socket(AF_UNIX, SOCK_DGRAM)
- rep_sock = socket(AF_UNIX, SOCK_DGRAM)
- req_sock_file = "/run/wpa_supplicant/wlan0"
- rep_sock_file = "/run/wpa_supplicant/reply"
- if not os.path.exists(req_sock_file):
- raise SystemExit("wpa_supplicant unix socket does not exist.")
- req_sock.connect(req_sock_file)
- rep_sock.bind(rep_sock_file)
- rep_sock.settimeout(1)
- yield rep_sock, req_sock_file
- req_sock.close()
- rep_sock.close()
- os.unlink(rep_sock_file)
- def parse(raw_bytes):
- text = raw_bytes.decode()
- result = {}
- for line in text.splitlines():
- key, value = line.strip().split("=", maxsplit=1)
- result[key] = value
- return result
- def main():
- with connect() as (sock, file):
- sock.sendto(b"STATUS", file)
- data = sock.recv(4096)
- result = parse(data)
- print(json.dumps(result))
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment