📜 ⬆️ ⬇️

Watchdog timer for 4G-modem on CentOS 7

This article is a supplement to my previous post about setting up a home router / file server. Here we will talk about the problem of automatic reconnection to the Internet when a 4G modem hangs. I do not pretend to the originality of the idea, I just want to share my decision with the readers.


In the process of constant work, about once a month, it happened that the USB modem was hanging. This gave me a certain inconvenience: the inability to remotely get on the home computer, to pre-load movies. It was impossible to eliminate this problem remotely; only a modem reboot by power helped. The only way out was to write a script that at a certain time checks the availability of a node on the Internet and, if necessary, reloads the modem.

I found several implementations on the Internet, but not one of them worked normally for me. Therefore, I decided to write my watchdog timer with preference and young ladies . The script was taken from this topic. Rewritten, as far as my qualification allows me, and added new features such as external options.

Installing and configuring a USB 4G modem in CentOS 7.
First you need to download the missing packages.
yum install usb_modeswitch usb_modeswitch-data 

Connect the modem and see how it is defined in the system.
 dmesg ip a 

Next, you need to configure the interface 4G modem.
 vim /etc/sysconfig/network-scripts/ifcfg-wwp6s0u1i1 

 DEVICE="wwp6s0u1i1" NAME="wwp6s0u1i1" TYPE="Ethernet" ONBOOT="yes" BOOTPROTO="dhcp" HWADDR="XX:XX:XX:XX:XX:XX" NM_CONTROLLED="no" DNS1=127.0.0.1 DNS2=127.0.0.1 DNS3=127.0.0.1 NOZEROCONF="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="no" ZONE="external" 

We create scripts to activate and deactivate the Internet when the interface is turned on or off.
 vim /sbin/ifup-pre-local 

 #!/bin/bash # PREUP="/etc/sysconfig/network-scripts/pre-up-${1:6}" if [ -x $PREUP ]; then exec $PREUP fi 

 vim /sbin/ifdown-pre-local 

 #!/bin/bash # PREDOWN="/etc/sysconfig/network-scripts/pre-down-$1" if [ -x $PREDOWN ]; then exec $PREDOWN fi 

 vim /etc/sysconfig/network-scripts/pre-up-wwp6s0u1i1 

 #!/bin/bash # echo -en 'AT^NDISDUP=1,1,"internet.yota"\r\n' > /dev/ttyUSB0 

 vim /etc/sysconfig/network-scripts/pre-down-wwp6s0u1i1 

 #!/bin/bash # echo -en 'AT^NDISDUP=1,0,"internet.yota"\r\n' > /dev/ttyUSB0 

Here ttyUSB0 is the modem port.
')
We raise the interface and check the connection.
 ifup wwp6s0u1i1 ip a 


The script itself
 #!/bin/bash export PATH="$PATH:/usr/sbin" SN="$(basename "$0")" function print_help() { printf "\n" printf ": %s options...\n" "$SN" printf ":\n" printf " -s  .\n" printf " -i   .\n" printf " -d     lsusb -t.\n" printf " -n   .\n" printf " -m  ,   lsusb.\n" printf " -h .\n" printf "\n" } #     ,  . if [[ $# = 0 ]]; then print_help && exit 1 fi while getopts ":s:i:d:n:m:h" opt ; do case $opt in s) SITE=$OPTARG; ;; i) IF=$OPTARG; ;; d) DEV=$OPTARG; ;; n) EP=$OPTARG; ;; m) MM=$OPTARG; ;; h) print_help exit 1 ;; *) printf " \n"; printf "    %s -h\n" "$SN"; exit 1 ;; esac done if [[ "$SITE" == "" ]] || [[ "$IF" == "" ]] || [[ "$DEV" == "" ]] || [[ "$EP" == "" ]] || [[ "$MM" == "" ]] ; then printf "\n" printf "     .\n" printf "  : %s -h\n" "$SN" printf "\n" exit 1 fi M="$(lsusb | grep -w "$MM")" #   lsusb if [[ "$M" != "" ]]; then #  ,    if grep -w -q "$IF" /proc/net/dev; then #    printf "\n" printf "  %s   %s\n" "$SITE" "$IF" printf "\n" if [[ "$EP" -ge 6 ]]; then printf "        5\n" exit 1 else printf " ...\n" flag="0" for i in {1..5}; do # 5    timeout -k 2 -s TERM 16 ping -w 14 -s 8 -c 1 -I "$IF" "$SITE" || flag=$((flag+1)) && printf ":%s/5 (:%s)\n" "$i" "$flag" #   -   if (("$flag" >= "$EP")); then break else read -r -t 1 > /dev/null fi done printf " : %s  %s\n" "$flag" "$i" printf "\n" if (("$flag" >= "$EP")); then #    2 M="$(lsusb | grep "$MM")" #     -    printf "  :\n" printf "%s\n" "$M" | cut -c 34- if ! [[ -d /sys/bus/usb/drivers/usb/"$DEV" ]]; then printf "  Bus  Port .\n" exit 1 else ifdown "$IF" #  printf "%s" "$DEV" > "/sys/bus/usb/drivers/usb/unbind" && printf "%s" "$DEV" > "/sys/bus/usb/drivers/usb/bind" #  # read -r -t 1 > /dev/null ifup "$IF" #  fi fi fi else printf "\n" printf " %s  \n" "$IF" printf "\n" exit 1 fi else printf " %s  .\n" "$MM" fi 


The script is located in / usr / local / bin /.
To start the script automatically, every five minutes, add the task to cron.
 crontab -e 

 */5 * * * * /usr/local/bin/watchdog -m Huawei -s ya.ru -i wwp6s0u2i1 -n 3 -d 1-1 > /dev/null 2>&1 

This is the output of dmesg, it shows a modem reset during the execution of the script.
 [181709.595498] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0 [181709.595568] option 1-1:1.0: device disconnected [181709.595798] huawei_cdc_ncm 1-1:1.1 wwp6s0u2i1: unregister 'huawei_cdc_ncm' usb-0000:06:00.0-1, Huawei CDC NCM device [181709.615005] option 1-1:1.0: GSM modem (1-port) converter detected [181709.616597] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0 [181709.623449] usb 1-1: MAC-Address: 0c:5b:8f:27:9a:64 [181709.623958] huawei_cdc_ncm 1-1:1.1: cdc-wdm0: USB WDM device [181709.624341] huawei_cdc_ncm 1-1:1.1 wwan0: register 'huawei_cdc_ncm' at usb-0000:06:00.0-1, Huawei CDC NCM device, XX:XX:XX:XX:XX:XX 

I’ll say right away that the script is very far from ideal, so I’m happy to accept advice and valid criticism.
Separately, I want to thank the user with Toster.ru under the nickname @AlekseyNemiro , for their help in optimizing the script.

UPD 01.22.16 Completed the script with the command to add the path to the PATH variable.

Source: https://habr.com/ru/post/275551/


All Articles