Skip to content

Instantly share code, notes, and snippets.

@heyalexej
Last active June 30, 2024 10:23
Show Gist options
  • Save heyalexej/cc6c97b1ea42736b3ff7 to your computer and use it in GitHub Desktop.
Save heyalexej/cc6c97b1ea42736b3ff7 to your computer and use it in GitHub Desktop.
SIM Card Management Through GSM Modem On Linux

SIM Card Management Over GSM Modem

A small guide on how to send and receive USSD codes on Linux

I am using the built in GSM (UMTS) modem of my Thinkpad X1 extensively because I am often in places with flaky internet connections. I connect through the standard Network Manager on Ubuntu and everything works fine. There was one major annoyance though. Every time I wanted to top up the SIM balance or book a new package, I needed a phone to send and receive USSD codes. So I took some time to figure out how to do it from the shell. I wrote this down as a help for others and a reminder for myself. Without further ado...

First intsall gammu and picocom.

~  sudo apt-get install -y gammu picocom

Obtaining infos about the device

~  dmesg | grep tty
[74804.272165] cdc_acm 3-4:1.1: ttyACM0: USB ACM device
[74804.273338] cdc_acm 3-4:1.3: ttyACM1: USB ACM device
[74804.301617] cdc_acm 3-4:1.9: ttyACM2: USB ACM device
[91522.989255] cdc_acm 3-4:1.1: ttyACM0: USB ACM device
[91522.989957] cdc_acm 3-4:1.3: ttyACM1: USB ACM device
[91523.017825] cdc_acm 3-4:1.9: ttyACM2: USB ACM device
[99661.279473] cdc_acm 3-4:1.1: ttyACM0: USB ACM device
[99661.280822] cdc_acm 3-4:1.3: ttyACM1: USB ACM device
[99661.309089] cdc_acm 3-4:1.9: ttyACM2: USB ACM device
[99958.320040] cdc_acm 3-4:1.1: ttyACM0: USB ACM device
[99958.321636] cdc_acm 3-4:1.3: ttyACM1: USB ACM device
[99958.349615] cdc_acm 3-4:1.9: ttyACM2: USB ACM device

➜  ~  lsusb | grep -i ericsson
Bus 003 Device 021: ID 0bdb:1926 Ericsson Business Mobile Networks BV 

➜  ~  lsusb -v -s 003:021

Bus 003 Device 021: ID 0bdb:1926 Ericsson Business Mobile Networks BV 
Couldn't open device, some information will be missing
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x0bdb Ericsson Business Mobile Networks BV
  idProduct          0x1926 
  bcdDevice            0.00
  iManufacturer           1 
  iProduct                2
  ...

➜  ~  for n in `ls /sys/class/*/*{ACM,wdm}*/device/interface`;do echo $(echo $n|awk -F '/' '{print $5}') : $(cat $n);done
ttyACM0 : H5321 gw Mobile Broadband Modem # this is what we're looking for
ttyACM1 : H5321 gw Mobile Broadband Data Modem
ttyACM2 : H5321 gw Mobile Broadband GPS Port
cdc-wdm0 : H5321 gw Mobile Broadband Device Management
cdc-wdm1 : Ericsson H5321 gw
cdc-wdm2 : H5321 gw Mobile Broadband USIM Port

We can now try to interact with the modem

~  sudo gammu --identify
Warning: No configuration file found!
Warning: No configuration read, using builtin defaults!
Device               : /dev/ttyACM0
Manufacturer         : Lenovo
Model                : unknown (H5321 gw)
Firmware             : R3C11
IMEI                 : 355001040455270
SIM IMSI             : 452010133540292

➜  ~  sudo picocom /dev/ttyACM0
picocom v1.7

port is        : /dev/ttyACM0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

*EMRDY: 1
AT+CPIN?
+CPIN: READY

OK
Thanks for using picocom

Cool, no pin needed in my case, makes things a lot easier. Now we can either run sudo gammu-config or create a config file manually under ~/.gammurc

~  cat ~/.gammurc           
# This is a generated gammurc file.
# It was generated by Gammu configurator 0.4

# In Unix/Linux  : copy it into your home directory and name it .gammurc
#                  or into /etc and name it gammurc
# In Win32       : copy it into directory with Gammu.exe and name gammurc

# Port           : in Windows/DOS: "com*:",
#                  (instead of "*" please put "1", "2", etc.)
#                  in other (Linux/Unix) "/dev/ttyS%"
#                                     or "/dev/ircomm%" ("irda" connection)
#                  (instead of "%" please put "0", "1", "2", etc.)
# Model          : use only, when Gammu doesn't recognize your phone model.
#                  Put it here. Example values: "6110", "6150", "6210", "8210"
# Connection     : type of connection. Use "fbus" or "mbus" or "dlr3" or
#                  "irda" (Infrared over sockets) or "infrared" (DirectIR)
#                  or "at19200" (AT commands on 19200, 8 bits, None parity,
#                  1 stop bit, no flow control) or "at115200" (AT commands on
#                  115200, 8 bits, None parity, 1 stop bit, no flow control)
#                  or "atblue" (AT over BlueTooth) or "dlr3blue" (FBUS
#                  over BlueTooth)
# SynchronizeTime: if you want to set time from computer to phone during
#                  starting connection. Do not rather use this option when want
#                  to reset phone during connection (in some phones need to
#                  set time again after restart)
# Logfile        : Use, when want to have logfile from communication.
# Logformat      : What debug info and format should be used:
#                  "nothing" - no debug level, "text" - transmission dump in
#                  text format, "textall" - all possible info in text format,
#                  "errors"  - errors in text format, "binary" - transmission
#                  dump in binary format
# Use_Locking    : under Unix/Linux use "yes", if want to lock used device
#                  to prevent using it by other applications
# GammuLoc       : name of localisation file

[gammu]
port = /dev/ttyACM0
connection = at
logfile = /var/log/gammu/gammu.log
logformat = textall

Now we can start to check the balance etc..

~  sudo gammu -c ~/.gammurc getussd "*122#" # check balance 
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "Your number is 2107970049.
Your Balance is 0 KIP.

➜  ~  sudo gammu -c ~/.gammurc getussd "*123#" # check data package
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "You don't have package."

➜  ~  sudo gammu -c ~/.gammurc getussd "*121*521248878287404#" # topping up
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "Recharging succeeded.The recharged amount is 50000 kip.Your balance is 50000  kip."

➜  ~  sudo gammu -c ~/.gammurc getussd "*122#" # check balance again
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "Your number is 2107970049.
Your Balance is 50000 KIP.

➜  ~  sudo gammu -c ~/.gammurc getussd "*131*6#" activate data package
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "Thank you for your register Package6 1Month U get free 5,000 MB."

➜  ~  sudo gammu -c ~/.gammurc getussd "*123#" # check data package again
Press Ctrl+C to break...
USSD received
Status               : No action needed
Service reply        : "Package6 Free Data: 5,120,000KB Expired 20/01/2015 15:26:47"

➜  ~  sudo gammu -c ~/.gammurc monitor 1 
Press Ctrl+C to break...
Entering monitor mode...

Enabling info about incoming SMS    : No error.
Enabling info about incoming CB     : No error.
Enabling info about calls           : No error.
Enabling info about USSD            : No error.
SIM phonebook        :   7 used, 993 free
Dialled numbers      :   0 used,  50 free
Received numbers     :   0 used,  50 free
Missed numbers       :   0 used,  50 free
Own numbers          :   0 used,   4 free
Phone phonebook      :   0 used, 500 free
Signal strength      : -61 dBm
Network level        : 78 percent
SIM SMS status       : 4 used, 0 unread, 30 locations
Phone SMS status     : 15 used, 0 unread, 30 locations

Leaving monitor mode...

➜  ~  sudo gammu -c ~/.gammurc getallsms
Location 100002, folder "Inbox", phone memory, Inbox folder
SMS message
SMSC number          : "+8562055003"
Sent                 : Fr 12 Dez 2014 01:33:39  +0700
Coding               : Default GSM alphabet (no compression)
Remote number        : "LTCRefill"
Status               : Read
User Data Header     : Concatenated (linked) message, ID (8 bit) 170, part 1 of 1

Your Package6 1Month U get free 5000MB will be expired on 13/12/2014.Please register new package after this package expired.Thank you. 

......

Location 100013, folder "Inbox", phone memory, Inbox folder
SMS message
SMSC number          : "+8562055003"
Sent                 : So 21 Dez 2014 15:10:36  +0700
Coding               : Default GSM alphabet (no compression)
Remote number        : "Lao Telecom"
Status               : Read

Thank you for your register Package6 1Month U get free 5,000 MB.

Location 100014, folder "Inbox", phone memory, Inbox folder
SMS message
SMSC number          : "+8562055003"
Sent                 : So 21 Dez 2014 15:10:44  +0700
Coding               : Default GSM alphabet (no compression)
Remote number        : "LTCRefill"
Status               : Read
User Data Header     : Concatenated (linked) message, ID (8 bit) 30, part 1 of 1

Hello, The rent fee for the Product Package6 1Month U get free 5000MB is 50000 KIP.

Location 100015, folder "Inbox", phone memory, Inbox folder
SMS message
SMSC number          : "+8562055003"
Sent                 : So 21 Dez 2014 15:11:21  +0700
Coding               : Default GSM alphabet (no compression)
Remote number        : "LTCRefill"
Status               : Read
User Data Header     : Concatenated (linked) message, ID (8 bit) 183, part 1 of 1

Dear   , you have  ordered the following Offering(s):   Package6 1Month U get free 5000MB effect on 2014-12-21, thank you.



19 SMS parts in 19 SMS sequences

Connection should work now. Based on these commands you can now go ahead and create shortcuts for all the commands. If entering your password annoys you, create an entry ìn sudoers.d like so sudo visudo -f /etc/sudoers.d/gammu with the this content username hostname = (root) NOPASSWD: /usr/bin/gammu.

Note that i didn't manipulate /etc/sudoers because it's a better practice and easier to maintain. As of debian version 1.7.2p1-1, the default /etc/sudoers file created on installation of the package now includes the directive /etc/sudoers.d. Read more about it in the man pages.

Add entries like these with your carriers USSD codes to your *sh_aliases or *shrc file.

alias gsm-balance='sudo gammu -c ~/.gammurc getussd "*122#"'
alias gsm-data='sudo gammu -c ~/.gammurc getussd "*123#"'
alias gsm-monitor='sudo gammu -c ~/.gammurc monitor 1'
alias gsm-sms='sudo gammu -c ~/.gammurc getallsms'

If you want to do this through a GUI, have a look at wammu. This is the GUI client to gammu. Apparently it has all the superpowers of Gammu but I don't like GUIs so I didn't try it. You can send and receive SMS through both as well.

As an alternative to picocom you can use cu (cu - call up another system) which I didn't try out myself.

Further reading:

https://wiki.debian.org/Modem/3G/Ericsson http://www.thinkwiki.org/wiki/Ericsson_F3507g_Mobile_Broadband_Module http://forum.ubuntuusers.de/topic/howto-aldi-umts-mobile-flat-guthaben-und-stat/ (german) http://www.techytalk.info/send-receive-sms-using-gsm-modem-phone-ubuntu/ http://blog.plenz.com/2011-12/x220s-umts-card.html

@hackerb9
Copy link

Thanks for the thorough how to! This looks like precisely what I'll be doing once I get my WWAN card installed in my laptop.

I laughed out loud when you suggested using cu. I haven't used tip or cu for years. I used to know them well because they were guaranteed to be installed on any UNIX system. I think *BSD still ships them by default, but tip and cu are additional downloads for most GNU/Linux distributions, so there's not much point learning their abstruse syntax.

Perhaps a more useful suggestion to people would be socat, which does often come preinstalled on GNU/Linux systems. Usage: socat stdio /dev/ttyACM0.

@eliakimatamba
Copy link

Cool. Mine worked just fine. Can you write an article on how to disable palm disruptions on ParrotSec Linux?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment