Last active
November 29, 2018 03:11
-
-
Save tuokri/8d0e3ef599441e3f52c1292ddf4176a9 to your computer and use it in GitHub Desktop.
wvdial automation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import configparser | |
import logging | |
import os | |
import serial | |
import signal | |
import sys | |
import subprocess as sp | |
import time | |
from logging.handlers import RotatingFileHandler | |
logger = logging.getLogger("autowvdial") | |
logger.setLevel(logging.DEBUG) | |
fh = RotatingFileHandler( | |
"/var/log/autowvdial.log", maxBytes=1024*1024, backupCount=2) | |
fh.setLevel(logging.DEBUG) | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.ERROR) | |
fh.setFormatter( | |
logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")) | |
ch.setFormatter( | |
logging.Formatter("%(levelname)s %(message)s")) | |
logger.addHandler(fh) | |
logger.addHandler(ch) | |
BAUDRATE = 9600 | |
TIMEOUT_DEFAULT = 60 | |
TIMEOUT = TIMEOUT_DEFAULT | |
WVDIAL_HANDLE = None | |
def wait_for_modem(modem_devfile): | |
logger.info("waiting for modem: %s") | |
start = time.time() | |
timeout = start + TIMEOUT | |
while (timeout >= time.time()): | |
if os.path.exists(modem_devfile): | |
logger.info("modem found") | |
return | |
time.sleep(0.05) | |
logger.error("timed out waiting for modem") | |
def enter_sim_pin(modem_devfile, pin): | |
logger.info("entering SIM PIN") | |
try: | |
with serial.Serial(modem_devfile, BAUDRATE) as ser: | |
ser.flushInput() | |
ser.flushOutput() | |
ser.write("AT+CPIN={}\r\n".format(pin).encode()) | |
buf = "" | |
last_recv = "" | |
start = time.time() | |
timeout = start + TIMEOUT | |
while (timeout >= time.time()): | |
buf += buf + ser.read(ser.inWaiting()).decode() | |
if "\r\n" in buf: | |
lines = buf.split("\r\n") | |
if lines[-2]: | |
last_recv = lines[-2] | |
buf = lines[-1] | |
if last_recv == "OK": | |
logger.info("SIM PIN entered succesfully") | |
return | |
else: | |
logger.error("received '%s' while entering SIM PIN", last_recv) | |
logger.error("message buffer: %s", buf) | |
return | |
logger.error("timed out entering SIM PIN") | |
except serial.SerialException as se: | |
logger.error("error entering SIM PIN: %s", se.strerror) | |
except Exception as e: | |
logger.error("error entering SIM PIN: %s", e) | |
def dial(dialer): | |
global WVDIAL_HANDLE | |
logger.info("dialing %s", dialer) | |
WVDIAL_HANDLE = sp.Popen( | |
["wvdial", dialer], stdout=sp.PIPE, stderr=sp.PIPE) | |
_, err = WVDIAL_HANDLE.communicate() | |
if err: | |
logger.error("error dialing %s: %s", dialer, err) | |
sys.exit(1) | |
def main(): | |
parser = argparse.ArgumentParser("Autodialer script for wvdial.") | |
parser.add_argument("modem") | |
parser.add_argument("pin") | |
parser.add_argument( | |
"-c", | |
"--wvdialconfig", | |
help="path to wvdial config file, default = %(default)s", | |
default="/etc/wvdial.conf") | |
parser.add_argument( | |
"-d", | |
"--dialer", | |
default="Dialer Defaults", | |
help="dialer entry in wvdial config file, default = %(default)s") | |
parser.add_argument( | |
"-t", | |
"--timeout", | |
type=int, | |
default=TIMEOUT_DEFAULT, | |
help="timeout for waiting modem and entering SIM PIN, default = %(default)s") | |
args = parser.parse_args() | |
global TIMEOUT | |
TIMEOUT = args.timeout | |
config = configparser.ConfigParser() | |
config.read(args.wvdialconfig) | |
logger.info("starting autowvdial, timeout=%s", TIMEOUT) | |
wait_for_modem(args.modem) | |
enter_sim_pin(args.modem, args.pin) | |
dial(args.dialer) | |
if __name__ == "__main__": | |
try: | |
main() | |
except KeyboardInterrupt: | |
logger.info("interrupt signal received, exiting") | |
try: | |
WVDIAL_HANDLE.send_signal(signal.SIGINT) | |
logger.info("sent SIGINT to wvdial") | |
except Exception as e: | |
logger.error("shutdown error: %s", e) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment