13-keepalive_modemmanager 2.5 KB
Newer Older
nickberry17's avatar
Init  
nickberry17 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
# Hotplug script for ModemManager events.  Place in /etc/hotplug.d/iface/13-keepalive_modemmanager
# and give execute permission.  
#
# Copyright 2019 Nicholas Smith <mips171@icloud.com>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

20
MODEMIFACE="mobiledata"
nickberry17's avatar
Init  
nickberry17 committed
21

Nicholas's avatar
Nicholas committed
22
23
24
25
26
27
modemstatus=$(/usr/bin/mmcli -m 0)
DISABLED=$(echo $modemstatus | grep -w "disabled")
SEARCHING=$(echo $modemstatus | grep -w "searching")
REGISTERED=$(echo $modemstatus | grep -w "registered")
CONNECTED=$(echo $modemstatus | grep -w "connected")
IDLE=$(echo $modemstatus | grep -w "idle")
nickberry17's avatar
Init  
nickberry17 committed
28
29
30
31

RUNNING="0" # Used to keep incidental events during the execution of one branch from triggering another branch.

if [ -n "$DISABLED" ]; then
Nicholas's avatar
Nicholas committed
32
	logger -t INFO	"Modem is disabled. Will attempt to initiate connection."
nickberry17's avatar
Init  
nickberry17 committed
33
34
35
	logger -t DEBUG "$DISABLED"
	if [ "$RUNNING" -eq "0" ]; then
		$RUNNING="1"
Nicholas's avatar
Nicholas committed
36
		ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
nickberry17's avatar
Init  
nickberry17 committed
37
38
39
40
41
42
43
44
	fi
fi

if [ -n "$CONNECTED" ]; then
	if [ "$(/bin/ping -I wwan0 -c 1 8.8.8.8 &> /dev/null ; echo $?)" -eq "1" ]; then # 1 is status code for host unreachable
		if [ "$RUNNING" -eq "0" ]; then
			logger -t INFO "Modem failed ping test. Reconnecting."
			RUNNING="1"
Nicholas's avatar
Nicholas committed
45
			ifdown $MODEMIFACE && ifup $MODEMIFACE && RUNNING="0"
nickberry17's avatar
Init  
nickberry17 committed
46
47
48
49
50
51
52
53
54
55
56
		else
			logger -t INFO "Modem is connected."
			logger -t INFO "$CONNECTED"
		fi
	fi
fi

if [ -n "$SEARCHING" ]; then
	if [ "$RUNNING" -eq "0" ]; then
		logger -t INFO "Modem is searching.  Attempting to bring modem back online."
		$RUNNING="1"
57
		ifdown $MODEMIFACE && ifup $MODEMIFACE && RUNNING="0"
nickberry17's avatar
Init  
nickberry17 committed
58
59
60
61
62
63
64
	fi
fi

if [ -n "$IDLE" ]; then
        if [ "$RUNNING" -eq "0" ]; then
                $RUNNING="1"
                logger -t INFO "Modem is idle.  Attempting to bring modem back online."
65
                ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
nickberry17's avatar
Init  
nickberry17 committed
66
67
68
69
70
71
72
        fi
fi

if [ -n "$REGISTERED" ]; then
	if [ "$RUNNING" -eq "0" ]; then
		$RUNNING="1"
		logger -t INFO "Modem is registered. Attempting to bring modem back online."
73
		ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
nickberry17's avatar
Init  
nickberry17 committed
74
	fi
Nicholas's avatar
Nicholas committed
75
fi