Wednesday, October 21, 2015

WVDIAL Keep Alive

Here's a script to make the wvdial keep alive. Similar to umtskeeper that use sakis3g, this one can be use for some modem or internet provider that sakis3g won't connect. let's call this script as runwvd.
runwvd: (moved to github)

https://github.com/huntu2/wvdial-keepalive/blob/master/runwvd

make the script executable
chmod a+x runwvd

change the MODEM_NAME to your modem vendor that shown by lsusb


To start do following:
sudo ./runwvd start

To stop:
sudo ./runwvd stop

To view progres:
tail -f /tmp/runwvd.log

Prerequisites:
Before above script able to run properly, you'll need package:
sudo apt-get install wvdial usb-modeswitch fping ppp nohup usbutils

configure wvdial.conf by executing
sudo wvdialconf

copy the result at /etc/wvdial.conf and place it in the same folder as this script. Complete the Phone number username and password, something like this
wvdial.conf:
#-----------------------------------------------
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2
Modem Type = Analog Modem
ISDN = 0
Modem = /dev/ttyUSB0
Baud = 9600
Phone = *99#
Username = '0'
Password = '0'
#------------------------------------------------


the '0' means no username and password supplied.

You also need resetusb, here's the source:
resetusb.c
#----------------------------------------------------
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
void main(int argc, char **argv)
{
    const char *filename;
    int fd;
    filename = argv[1];
    fd = open(filename, O_WRONLY);
    ioctl(fd, USBDEVFS_RESET, 0);
    close(fd);
    return;
}
#----------------------------------------------------

compile it using:
gcc -o resetusb resetusb.c

Brief how it works:

The runwvd start with stamp file (/tmp/.runwvd) to control the loop, if this file gone, the loop will stop and the script terminated. The output process can be shown at /tmp/runwvd.log and the wvdial progress can be monitored from /tmp/wvdial.log.
 

First, it will call wvdial with wvdial.conf at the same folder. Then every SLEEPTIME period (above is 3 second) will regularly check for ppp0 existence and wvdial process. If both is missing, then the script will kill the wvdial and redial again.
 

Second, if first step is passed , the script will try to ping the dns server on peride CHECKTIME x SLEEPTIME, (above every 3x60=3minutes). The IP of the DNS were taken from /etc/resolv.conf resulted by wvdial upon successful connection. The ping loop will try to reach the DNS server for PINGLIMIT times (above is 5) with 1 second delay each. If all fail, then the script will terminate the wvdial and redial again.
 

Third, if the redial process happen more than REDIALIMIT (above is 5 times), then the script will try to reset the 3G modem after the wvdial terminated, and then redial again. This will help to re-register the 3G modem to the mobile network base station.
 

This note was written for my own record continuing previous blog on my homemade 3g router, but this might be useful for others who need it. 
Sorry for the bad blog format due to  my poor knowledge of the HTML and english language.

3 comments:

  1. Excellent work! Testing this on my Novatel!

    ReplyDelete
  2. Very useful script. How does this work with Auto Reconnect option for wvdial config? Do we need to turn that off, in order for runwvd to work well?

    ReplyDelete
  3. #!/bin/bash
    i=1
    while [ $i -le 10 ];
    do
    sudo modprobe usbserial vendor=0x product=
    sudo usermod -aG dialout $USER
    sleep 2
    sudo wvdial
    sleep 10
    $i
    let $[ i+=1 ]
    # you can stop this process with 'Ctrl+C'

    ReplyDelete