[dd-wrt] Check internet balance of 3G Modem via USSD

Post Reply
User avatar
hiccup
Site Admin
Posts: 19
Joined: Sat Aug 01, 2015 8:44 pm

[dd-wrt] Check internet balance of 3G Modem via USSD

Post by hiccup » Fri Jan 22, 2016 1:32 pm

So I was using Huawei e303 3G Modem with my TPLINK WR1043ND V2 router which is flashed with DD-WRT v3.0-r28586.
I needed to check remaining balance of my internet package.

I've searched around but couldn't find any solution that could work out of the box.
The code from here dialed correctly and received reply but it was encoded in 7-bit GSM.
So I needed to decode it to make it readable.

You can ofcourse use online pdu decoder or javascript pdu decoder.
But I needed it to decode it instantly to make it simpler.

So I built this script that will dial a USSD number and decode the 7-bit gsm reply.

to use this script, enable jffs2 in your router and the upload it in /jffs/
then from terminal

Code: Select all

chmod +x balance.sh
to see the balance, simply execute the script in terminal.
or you may use the

Code: bash Select all

#!/bin/sh
rm -f /jffs/bal.txt
echo "Dialing *566*10#.."
cat /dev/usb/tts/2 | grep +CUSD: | sed "s/.*\"\([A-F0-9]*\)\".*/\1/" > /jffs/bal.txt &
echo -e "AT+CUSD=1,\"AA9ACDA68AC146\",15\r\n" > /dev/usb/tts/2
sleep 10
killall cat
text=$(cat /jffs/bal.txt)

l=${#text}
echo "decoding reply..."
while [ $l -gt 0 ]
do
	l=$(($l-2))
	h="0x${text:$l:2}"
	DecNum=`printf "%d" $h`
	Binary=
	while [ $DecNum -ne 0 ]
	do
	  Bit=$(expr $DecNum % 2)
	  Binary=$Bit$Binary
	  DecNum=$(expr $DecNum / 2)
	done
	le=${#Binary}
	while [ $le -lt 8 ]
	do
	  Binary="0$Binary"
	  le=$(($le+1))
	done
	
	bin=$bin$Binary
	unset Binary
done

l=${#bin}

while [ $l -gt 0 ]
do
	l=$(($l-7))
	h="0${bin:$l:7}"
	
	Binary="0${bin:$l:7}"
	Decimal=0
	power=1

	while [ $Binary -ne 0 ]
	do
	  rem=$(expr $Binary % 10 )
	  Decimal=$((Decimal+(rem*power)))
	  power=$((power*2))
	  Binary=$(expr $Binary / 10)
	done

	hexnum=`printf \\\\x"%X" $Decimal`
	echo -n -e $hexnum

done
echo ""
source: I've taken help from several places. These are worth mentioning:
ussd dial
SO
base convertion

Post Reply