I have an Orange Pi that connects to the network via WiFi, and since my network is not very stable, the wireless router side often forcefully disconnects all devices, requiring manual reconnection. This is an annoying thing when I'm out: first, I have to figure out how to SSH to my Pi!
The network manager I use is NetworkManager, which is very modern and user-friendly. Unfortunately, until today its developers were not interested in adding an option to automatically reconnect to hotspots.
In this issue, some developers have suggested using the dispatcher to accomplish this. Well... I wrote a simple script to do this. It will try to restart the NetworkManager service 3 times when the network is disconnected (down event received).
There's not much hard-coding in the script, so it should work on anyone's device.
Note that the script requires at least Python 3.10 because it uses match statement, which was introduced since Python 3.10.
Put it under your /etc/NetworkManager/dispatcher.d/:
sudo cp network-manager-auto-reconnect.py /etc/NetworkManager/dispatcher.d/
sudo chmod +x /etc/NetworkManager/dispatcher.d/network-manager-auto-reconnect.pyAnd it is all set. You should not need to restart your device or any service to make it effective. It takes effects immediately.
You can trigger a disconnection and see if it works:
sudo nmcli connection down <connection name. Usually your SSID name>