Syed Jahanzaib Personal Blog to Share Knowledge !

May 28, 2015

Modem Status and SIM Balance info via EMAIL or SMS

Filed under: Linux Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 10:27 AM

 


 

Task:

Requirement:

Daily reporting for the Modem status and SIM balance via email (or sms)
In this example I had TELTNOKIA SERIAL Modem (Model: Teltonika ModemCOM/G10 Serial ) 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 created this script which run daily in morning  sends email info / 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 fulfilled my custom requirement with my text.

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 2.0 / Modified on 25-08-2015

# Stop Kannel and playsms
# EMAIL RELATED STUFF
TO1="aacableathotmaildotcom"
GMAILID="YOURGMAILID@gmail.com"
GMAILPASS="GMAILPASSWORD"
CONTENT_TYPE="text/html"
MAILFILE="/tmp/mail.txt"
COMPANY="ZAIB"
CURDATE=`date`

# STOP KANNEL so that GAMMU CAN RUN
service kannel stop
sleep 2
killall -9 bearerbox

# Check for temporary files that might be created by earlier
clear
echo -e "$CURDATE Checking for old garbage files created earlier tby this script"
> /tmp/modeminfo

# 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.
A=`grep TELTONIKA "/tmp/modeminfo"`
if [ "$A" == "" ]; then
echo -e "$CURDATE Modem NOT Responding.."
echo -e "$CURDATE Modem NOT Responding.." > $MAILFILE

# START KANNEL which was stopped earlier
service kannel start
/temp/sendEmail-v1.56/sendEmail -t $TO1 -u "ALERT: $CURDATE Modem Daily Report: $COMPANY  Modem NOT RESPONDING!" -o tls=yes -s smtp.gmail.com:587 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MAILFILE  -o message-content-type=$CONTENT_TYPE

exit 0
else

# If modem found, then execute USSD code to inquire Mobile SIM Balance in tmp file (FOR ZONG SERVICE)
echo -e "Modem seems to be responding OK"
echo -e "================================="
echo -e "Now executing USSD code FOR ZONG MOBILE SERVICE via GAMMU"
gammu --getussd *222# > /tmp/mobilebal

# Look for word insufficient in above output file, if found then give error and exit.
B=`grep insufficient "/tmp/mobilebal"`
if [ "$B" == "" ]
then

BAL=`cat /tmp/mobilebal  |awk 'FNR == 3 {print $8}' |cut -f1 -d"."`
echo -e "Balance & Modem both OK, Alhamdolillah"
echo -e "Balance & Modem both OK, Alhamdolillah" > $MAILFILE
echo -e "Balance is:  $BAL"
echo -e "Balance is  $BAL  RS" |cut -f1 -d"." >> /tmp/mail.txt
/temp/sendEmail-v1.56/sendEmail -t $TO1 -u "INFO: $CURDATE Modem Daily Report is OK with balance of $BAL" -o tls=yes -s smtp.gmail.com:587 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MAILFILE  -o message-content-type=$CONTENT_TYPE

# START KANNEL which was stopped earlier
service kannel start

exit 0

# ELSE print modem and sim status
else
echo -e "Modem Reponding but ZONG SIM may have not enough balance"
echo -e "Modem Reponding but ZONG SIM may have not enough balance" > /tmp/mail.txt
/temp/sendEmail-v1.56/sendEmail -t $TO1 -u "ALERT: $CURDATE Modem Daily Report: $COMPANY  Modem OK BUT SIM have no Balance" -o tls=yes -s smtp.gmail.com:587 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MAILFILE  -o message-content-type=$CONTENT_TYPE

# START KANNEL which was stopped earlier
service kannel start

fi
fi
## START


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

%d bloggers like this: