pinephone-scripts/modem-restart

66 lines
1.1 KiB
Bash
Executable File

#! /bin/sh
is_modem_available()
{
if [ -z "$(lsusb | grep "2c7c:0125")" ]; then
return 1
fi
return 0
}
is_data_available()
{
if ! nmcli c show --active | grep -e internet -e gsm > /dev/null; then
return 1
fi
return 0
}
connectivity()
{
if nmcli c show --active | grep wlan0; then
return 0
fi
if [ $COUNTER -ge 10 ]; then
COUNTER=0
if ping -q -W5 -c1 -s0 libredns.gr; then
return 0
else
return 1
fi
fi
COUNTER=$((COUNTER + 1))
return 0
}
TRY=0
COUNTER=0
while :; do
if ! is_modem_available ; then
TRY=$((TRY + 1))
echo $(date) Restarting modem, $TRY try
systemctl restart eg25-manager
systemd-inhibit --what=sleep --why="Modem reset" sleep 60
elif [ $TRY -ne 0 ]; then
echo $(date): Modem restarted successfully
TRY=0
elif ! is_data_available; then
echo $(date): Restarting mobile data
nmcli c down internet
nmcli c up internet
systemd-inhibit --what=sleep --why="Restart mobile data" sleep 5
elif ! connectivity; then
echo $(date): Connectivity test failed. Restaring modem...
systemctl restart eg25-manager
systemd-inhibit --what=sleep --why="modem reset" sleep 60
TRY=$((TRY + 1))
fi
sleep 5
done