Package: netswitch
NetswitchNetwork switching. Make sure you're connecting with the priority that you want.Works on Linux (relies on wpa_supplicant)InstallpipinstallnetswitchDockerOr you can use Docker instead:# copy your wpa supplicant configscp-r./aps/etc/wpa_supplicant/aps

dockerrun-d--privileged--network="host"\-v/etc/wpa_supplicant:/etc/wpa_supplicant\-eLIFELINE_SSID=mylifeline-2G\--namenetswitchbeasteers/netswitchwhere./apsis filled with a single wpa_supplicant network configuration and where the filename matches the SSID that's inside the file:# all of the networks you want to be able to connect to
./aps/
    networkA.conf
    myspectrum-2G.conf
    myspectrum-5G.conf
    ...You can also use-v ./aps:/etc/wpa_supplicant/apsinstead of copying if you want the aps to be editable from their current location.IMPORTANT: if you don't give it any aps to manage, it won't do anything.UsageNetwork Switchingimporttimeimportnetswitch# tell netswitch where to get its AP list. see explanation belownetswitch.set_ap_path('path/to/aps')# create a new wpa supplicant filenetswitch.generate_wpa_config('my-network-2G','wifipassword')# create your network switching ruleswitch=netswitch.NetSwitch([{'interface':'wlan*','ssids':'lifeline'},'eth*',# equivalent to {'interface': 'eth*'}'ppp*',{'interface':'wlan*'},# implied - 'ssids': '*'])# periodically check networkwhileTrue:time.sleep(10)connected=witch.check()# or equivalentlywitch.run(interval=10)For example, assume your setup is:ifaces: wlan0 (built-in Pi 4 wifi), wlan1 (usb wifi dongle), ppp0 (cellular)trusted ssids: nyu, nyu-legacyProcedure - at any point in these steps, if we connect, we're finished:check wlan1 for s0nycL1f3l1ne, if yes, connectcheck wlan0 for s0nycL1f3l1ne, if yes, connectcheck for eth0 and internet connected thru interfacecheck for ppp0 and internet connected thru interfaceshould we control wvdial or whatevs in this ???check wlan1 for [nyu, nyu-legacy], if yes, connectcheck wlan0 for [nyu, nyu-legacy], if yes, connectcheck if internet is already connected thru any interfaceModifications to WPA SupplicantFor the collection of available APs and credentials that you can connect to, typically they are stored in a single/etc/wpa_supplicant/wpa_supplicant.conffile and the wpa service will select the best one based on the priority specified. But this has issues and doesn't switch when one network is stronger than the other.So, what's different:instead of one big file, we break it up into one file per AP (where the filename is the same as the AP ssid)periodically, the above check will be run and we will check to see what networks are in range, which ones are trusted, and which have the highest quality. If a different AP consistently higher quality (3/5 pings atm), then we will switch to the new AP.So callingnetswitch.sync_aps('path/to/aps')tellsnetswitchthat each of your AP credentials are stored in individual files under'path/to/aps'.You can also callnetswitch.sync_aps('path/to/aps')which will sync that directory with/etc/wpa_supplicant/aps/and will operate out of there so that nothing will be touched in the original directory.CLI# run the wifi switcher with a lifeline ssid to look out forpython-mnetswitchrun--lifelinemylifelinenetwork-2G# get ip addresses for available interfacespython-mnetswitchip
python-mnetswitchip'wlan*''eth*'# certain interfaces# get available apspython-mnetswitchaps
python-mnetswitchapsen0# for mac# get available interfacespython-mnetswitchiface
python-mnetswitchiface'*tun*''eth*'# restart interfacepython-mnetswitchrestartwlan0# list current wpa supplicant infopython-mnetswitchwpainfoWPA Supplicantimportnetswitch# get current wpa supplicant filewpa=netswitch.Wpa()# getCellularimportnetswitch.cellassertisinstance(netswitch.cell.signal_strength(),float)withnetswitch.cell.Cell('/dev/ttyUSB2')ascell:# read from stdindcell.chat()TODOtests - .travis.ymleth0 connection, ppp0 connection/etc/network/interfaces setup? / instructions?restart interface if it's missing / not connected? maybe only 0, or if it showed up once?time since connected - restart after a certain amount of time./etc/network/interfaces????