That is what wpa_supplicant does... Don't really need the script to do that, after you get the first one working, just edit the file. In the /etc/wpa_supplicant/wpa_supplicant.conf file you can list a bunch of networks and it will follow them by priority and by what it finds first: ctrl_interface=D...