Here is the script that I currently use, it improves greatly the usability of my phone, because it reduces dramatically the need to go in the settings in order to get access to the Internet.
Though it's not perfect and might have secondary effects, use at your own risks!
#!/bin/bash
MODEM="/ril_0"
HOST_TO_PING="8.8.8.8 www.google.com ubports.com 1.1.1.1 9.9.9.9 cloudflare.com amazon.com microsoft.com wikipedia.org apple.com"
function get_modem_iface()
{
/usr/share/ofono/scripts/list-contexts |awk -v target="$1" '
/^\[ \/ril_/ { r=$2 }
/Type = internet/ { i=1 }
/Interface=ccmni/ && r==target && i {
sub(/^.*Interface=/, "")
sub(/ .*/, "")
print
exit
}
'
}
IFACE=$(get_modem_iface $MODEM)
i=0;
while true; do
if [ "$IFACE" = "" ]; then
IFACE=$(get_modem_iface $MODEM)
fi
ifconfig $IFACE | grep -v inet6 | grep inet > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
gdbus call --system --dest org.ofono --object-path "$MODEM" --method org.ofono.NetworkRegistration.GetProperties | grep "'Technology': <'lte'>" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
date
echo "IFACE: $IFACE";
echo "IP loss detected"
/usr/share/ofono/scripts/offline-modem $MODEM
/usr/share/ofono/scripts/online-modem $MODEM
sleep 5;
IFACE=$(get_modem_iface $MODEM)
fi
else
route=$(ip route show default)
if [ "$route" = "" ]; then
gdbus call --system --dest org.ofono --object-path "$MODEM" --method org.ofono.NetworkRegistration.GetProperties | grep "'Technology': <'lte'>" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
date
echo "IFACE: $IFACE";
echo "Route loss detected"
/usr/share/ofono/scripts/offline-modem $MODEM
/usr/share/ofono/scripts/online-modem $MODEM
sleep 5;
IFACE=$(get_modem_iface $MODEM)
fi
fi
fi
i=$((i+1))
if [ "$i" -gt "15" ]; then
i=0;
gdbus call --system --dest org.ofono --object-path "$MODEM" --method org.ofono.NetworkRegistration.GetProperties | grep "'Technology': <'lte'>" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
internetaccess=0;
for host in $HOST_TO_PING ; do
ping -W 1 -c 1 $host >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
internetaccess=1;
break;
else
echo "Ping to $host failed, trying next one in 4s ...."
sleep 4;
fi
done
if [ "$internetaccess" -eq "0" ]; then
date
echo "IFACE: $IFACE";
echo "Internet access loss detected"
/usr/share/ofono/scripts/offline-modem $MODEM
/usr/share/ofono/scripts/online-modem $MODEM
sleep 5;
IFACE=$(get_modem_iface $MODEM)
fi
fi
fi
sleep 20;
done