USSD BROWSING The text-based internet services that Tilde users like to play with are all very interesting, but there's also more to text than the internet. If I was cooler I might now go on to talk about the Hellschreiber or something, but instead it's Unstructured Supplementary Service Data, or USSD. I discovered this (or at least the details) after my mobile broadband internet provider (Telstra, the big ex state-owned telco that us Australians love to hate) discontinued the simple URL that you could visit to view your remaining internet data and expiry. Now you've got to create an account and log in, and I don't really want to do that because I'm kind-of ripping them off to get my cheap internet. But you could still get a balance via the #100# code on a mobile phone, and that turns out to be one of these USSD codes. It's a special protocol for phones to query the telco's system for a text response. The request code is of the structure *[code][*[selector]...]#, where eg. (with Telstra) *100# gives you a menu, then *100*2# selects the second option from that menu, and you can go down deeper through the menus with more *[number] sequences before the hash. Looking through old forum posts, it seems that Telstra has had a couple of systems for presenting interesting content via this service, from the time before phones could access the internet. You used to be able to pull up news, weather, and some other stuff. Potentially an easy way to get some daily plain-text info without trying to strip it out of bloated websites. But I'm late to the party, these days this is all you get from *100#: --------------------------------- Pre-Paid Mobile Broadband Exp 25 May 2021 1. Recharge: credit/debit card 2. Recharge: voucher 3. Balance Details 4. Account Management --------------------------------- Or *150# shows you your phone number. So yeah, back to just finding out my remaining internet data then... I'm using a USB mobile broadband modem (one of the old style ones that doesn't just present as a USB ethernet adapter) so I need to use AT commands, where it turns out there's a special AT+CUSD command for USSD "browsing". There may be pre-baked ways to do this with software like ModemManager, but I was looking for a solution for the software already installed on the 8MB flash of my router running OpenWrt, so here's the script I came up with: ------------------- #!/bin/sh #Get Telstra account usage/balance data #Converts from byte codes because Telstra doesn't output strings in plain text for some reason. # USSD code either specified on command line, or value set below is used. #USSD Code: # "*100#" shows the menu, adding "*3" selects the third menu item (can repeat for deeper menu levels) [ "$1" ] && BALCODE="$1" || BALCODE='*100*3#' TMP=/tmp/baldata.txt MODEM=/dev/ttyUSB1 OLDTTY="`stty -g`" stty -F $MODEM 9600 raw chat -e -t 20 -r "$TMP" \ REPORT CUSD: \ '' AT \ OK\\r \ AT+CUSD=1,\""$BALCODE"\",15 \ OK\\r \ '' \ ,72\\r \ < $MODEM > $MODEM stty "$OLDTTY" echo "--------------------------------------------------------------------------" if [ -f "$TMP" ] then expr "`cat "$TMP"`" : '.*CUSD: [01],"\([0-9A-F]*\)",72' | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf echo rm "$TMP" else echo "--Error: No Response--" fi ------------------- It turns out Telstra encode all the text in hexadecimal and then provide that as the ASCII string that's returned, so I found the sed + xargs + printf converter process on Stack Overflow (thank you Stack Overflow posters who always try to solve every problem with standard shell commands). So with that I get a response like: ------------------- AT OK AT+CUSD=1,"*100*3#",15 OK +CUSD: 1,"0044006100740061003A00200032002E003900380036004700420020004500780070003A00320035002F00300035002F0032003000320031002000320033003A00350039003A00350039000A000A002800410045005300540029000A00300030002E00200048006F006D0065",72 -------------------------------------------------------------------- Data: 2.986GB Exp:25/05/2021 23:59:59 (AEST) 00. Home ------------------- The chat script doesn't have to echo to the terminal of course (remove the "-e" argument), but I quite like seeing the process going on. Also, I couldn't find any docs explaining what the ",72" at the end of the response string is supposed to mean, even though I'm using it to detect the end of the response in the chat script. It seems to work reliably for Telstra. As an extra bonus, here's a simple script for sending a text message from your computer via a mobile broadband modem: ------------------- #!/bin/sh #Send SMS # Syntax: sendsms.sh [phone number] [message] MODEM=/dev/ttyUSB1 OLDTTY="`stty -g`" stty -F $MODEM 9600 raw chat -e \ '' AT \ OK\\r \ AT+CMGF=1 \ OK\\r \ AT+CMGS=\""$1"\" \ '> ' \ "$2"^z \ OK\\r \ < $MODEM > $MODEM stty "$OLDTTY" ------------------- - The Free Thinker