Quantcast
Channel: Syed Jahanzaib – Personal Blog to Share Knowledge !
Viewing all articles
Browse latest Browse all 409

Modem Status and SIM Balance check with Mail Alert

$
0
0

dlink


 

Task:

Requirement of daily reporting for the Modem status and SIM balance via email.
In this example I had DLINK DWM-156 USB Modem attached with Linux base system [Ubuntu 12.4].
This system had kannel/playsms installed which sends sms alerts for various events and can trigger different scripts upon receiving SMS with specific codes or instructions. I found that this modem was cheap and had many issues like not responding or likewsise, so I created this script which runs few times a day and sends email alerts for any issue related to modem and sim balance. on internet there might be some better guides or methods to do the same task, but this one suites my custom need. at a moment its on a very basic level, later we will modify it if required further :)
Sharing this script, maybe some one will get benefit from it or improve it on a pro level.


 

Requirement:

GAMMU must be installed and must be configured with the modem. you should run gammu –identify to make sure its detecting  the modem.

 


 

Bash Script to do the job:

######################
cat /temp/checkbal.sh
######################

#!/bin/bash
# Script to check Modem status and SIM balance via GAMMU and send email alerts accordingly.
# Syed Jahanzaib / aacable @ hotmail.com
# http//aacable.wordpress.com
# Version 1.0 / Modified on 28-5-2015

## START
# Check for temporary files that might be created by earlier, upto you : )
echo -e "Checking for old garbage files created earlier tby this script"
#[ -f /tmp/modeminfo ] && rm -f /tmp/modeminfo > /dev/null
#[ -f /tmp/mobilebal ] && rm -f /tmp/mobilebal > /dev/null

# Identify MODEM and output its result in tmp file
echo -e "Checking MODEM status"
gammu --identify > /tmp/modeminfo

# Look for Modem string , if not found give error and exit. MTK1 is my dlink modem string, you should change it with your modem string
A=`grep MTK1 "/tmp/modeminfo"`
if [ "$A" == "" ]; then
echo -e "Modem NOT Responding. Now scheduling USBRESET.SH file which will RESET the USB device & then re-run this script again"
echo -e "Modem NOT Responding. Now scheduling USBRESET.SH file which will RESET the USB device & then re-run this script again" > /tmp/mail.txt
# Email the 'modem not responding' result to ADMIN
mail -s 'Galaxy USB Modem NOT responding' aacable-hotmail-com < /tmp/mail.txt

# Running usb reset SCRIPT ,upto you, i made this usbreset.sh which reset the USB device and the modem start working again if its in HANG position, works for me
# /temp/resetusb.sh

#Scheduling current script to run after one minute again to see if modem is detectable after reset.
#echo /temp/checkbal.sh | at now + 1 min
else

# If modem found, then execute USSD code to inquire Mobile SIM Balance in tmp file, in this case I am using ZONG mobile sim
echo -e "Modem seems to be responding OK"
echo -e "================================="
echo -e "Now executing ZONG USSD code via GAMMU to inquire balance "
gammu --getussd *222# > /tmp/mobilebal

# Look for word insufficient in above output file, if not found then all ok.
B=`grep insufficient "/tmp/mobilebal"`
if [ "$B" == "" ]
then
# Print Mobile Balance, modify it accordingly
BAL=`cat /tmp/mobilebal |awk 'FNR == 3 {print $5}' `
echo -e "Balance & Modem both OK, Alhamdolillah"
echo -e "Balance & Modem both OK, Alhamdolillah" > /tmp/mail.txt
echo -e "Balance is $BAL PKR"
echo -e "Balance is $BAL PKR" >> /tmp/mail.txt
mail -s 'Daily Report: Galaxy USB Modem & SIM Balance is OK' aacable-hotmail-com < /tmp/mail.txt

# ELSE print modem and sim status
else
echo -e "Modem Responding but SIM have not enough balance"
echo -e "Modem Responding but SIM have not enough balance" > /tmp/mail.txt
mail -s 'Daily Report: Galaxy USB Modem OK BUT SIM have no Balance' aacable-hotmail-com < /tmp/mail.txt

fi
fi


Result:

CLI result:

Modem responding, but SIM have no balance

 

modem-not-balance

 

Modem responding, & SIM also have enough balance

report3-cli

 

Email Result:

 

MODEM-BAL-OK

Report1

 

 

 


 

Its just rough notes, you must modify things according to your network and requirements.

Regard’s
Syed Jahanzaib


Filed under: Linux Related

Viewing all articles
Browse latest Browse all 409

Trending Articles