[PYTHON] Let's set and set raspberry wifi in cui script

Purpose

Set the network so that it can be connected to the spare wifi network on the remote Raspberry Pi (connected to the network with ak20). Alternatively, enter the customer's network settings in advance in the Raspberry Pi network settings.

manner

Edit wifi config file


sudo vim /etc/wpa_supplicant/wpa_supplicant.conf

/etc/wpa_supplicant/wpa_supplicant.conf


ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP

network={
    ssid="W03_aaaaa" #wifi name
    psk="aaaa"  #wifi password
    key_mgmt=WPA-PSK 
}

network={
    ssid="W03_bbbb" #wifi name
    psk="bbbbb"  #wifi password
    key_mgmt=WPA-PSK
    priority=3  #Priority: Priority is given to the larger one to connect default 0
    disabled=1  #Whether to connect automatically default 0 that does not connect automatically if it is 1.
}

script By writing a script that rewrites wpa with reference to the above, it will be easier to set the Raspberry Pi for the customer. (Can only be executed with ⚠️sudo privileges)

add_wifi_ssid.py


import fire


def main(ssid: str, password: str):
  file = "/etc/wpa_supplicant/wpa_supplicant.conf"
  with open(file, 'a') as f:
    text = "network={\nssid=\"" + ssid + "\"\npsk=\"" + password + "\"\nkey_mgmt=WPA-PSK\n}"
    print(text, file=f)


if __name__ == '__main__':
  fire.Fire(main)

Other

Command to switch wifi with cui


networksetup -setairportnetwork en0 $ssid $password

Recommended Posts

Let's set and set raspberry wifi in cui script
Let's run a Bash script in Java