Skip to content

Instantly share code, notes, and snippets.

@jacobpledger
Created August 8, 2020 03:07
Show Gist options
  • Save jacobpledger/7a93a8467481cb286bd559c68c2194ab to your computer and use it in GitHub Desktop.
Save jacobpledger/7a93a8467481cb286bd559c68c2194ab to your computer and use it in GitHub Desktop.
Run this after flashing the OS image to a Raspberry Pi SD card to have it connect to your WiFi automatically, allowing headless configuration.
#!/usr/bin/env python3
import argparse
import os
import platform
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--ssid",
type=str,
required=True,
help="The SSID (name) of the network you want the Pi to connect to.",
)
parser.add_argument(
"--psk",
type=str,
required=True,
help="The pre-shared-key (password) to connect to your desired network.",
)
parser.parse_args()
running_on = platform.system()
if running_on == "Darwin":
device = "/Volumes/boot"
ssh = f"{device}/ssh"
wpa_supplicant = f"{device}/wpa_supplicant.conf"
else:
quit("I don't know what the mount-point is on non-Macs, sorry.")
os.makenod(ssh)
with open(wpa_supplicant, "w") as f:
f.write(
f"""
country=CA
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network=\{
ssid={ssid}
psk={psk}
\}
"""
)
os.system(f"umount {device}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment