Geek magazine hacker daily blog

8 years, 4 months ago
Thanks to the author of a topic Account check Kiyevstar of the modem in Linux which helped me to understand the principle of operation of modems and mode of work with them in Linux. I needed to solve a problem of display of the signal level, check of balance, account replenishment with use 3zh the modem. But as there was no wish to put on home machines pkhp for the sake of such trifle to me, decided to rewrite a script under himself on Python which is in any a desktop Linux from a box.

This script will work at any modems / phones which support USSD without coding PDU. For modems of the commands demanding the PDU coding it is necessary to write functions wrappers similar to PHP script from the topic stated above. I unfortunately have no such modem and I could not check — therefore did not begin to write.

Work on 3G the ZTE MF100 modem (mf626, mf628 has to work with mf620, mf622) and a rate the Internet 3G from Kiyevstar is checked.

Script of
import binascii 
import sys 
  f = open (" / dev/ttyUSB2""r+")
error= "Usage:    python action code\r\nActions: balans, popolnit, data-status, 3g-data-status, signal"
if len(sys.argv)  < 2:
    print error 
    sys.exit ()
if sys.argv[1]  == 'balans':
    print>> f, "AT+CUSD=1,*111#,15 \r\n"
    while data[:5] !=" + CUSD":
        data=f.readline ()
    data = data[10:-6]
    print binascii.unhexlify(data) 
elif sys.argv[1]  == 'signal':
    print>> f, "AT+CSQ\r\n"
    while data[:5] !=" +CSQ:":
        data=f.readline ()
    data = data[6:-5]
    sig_str = - 113+int (data)*2
    sig_per = int (data) *100/31  
    print unicode(sig_str)+ "dBm / " +unicode(sig_per)+ "%"
elif sys.argv[1]  == 'popolnit':
    print>> f, "AT+CUSD=1, *123 *" +sys.argv [2]+ "#,15 \r\n"
    while data[:5] !=" + CUSD":
        data=f.readline ()
    data = data[10:-6]
    print binascii.unhexlify(data) 
elif sys.argv[1]  == 'data-status':
    print>> f, "AT+CUSD=1,*121#,15 \r\n"
    while data[:5] !=" + CUSD":
        data=f.readline ()
    data = data[10:-6]
    print binascii.unhexlify(data) 
elif sys.argv[1]  == '3g-data-status':
    print>> f, "AT+CUSD=1,*122#,15 \r\n"
    while data[:5] !=" + CUSD":
        data=f.readline ()
    data = data[10:-6]
    print binascii.unhexlify(data) 
    print error 

We do a script performed:
chmod +x

. / action (code)
Available actions: balans, popolnit, data-status, 3g-data-status, signal
At the choice of action of popolnit the script needs to transfer by the second parameter a replenishment code.
. / popolnit 1234567890

The script is ground under Kiyevstar, but I think at anybody it is possible to replace short numbers of requests on used by your operator.
There is a script "suspends" as far as I understood it occurs when he does not wait for the necessary answer from the modem. I sin on a bad covering in my area. If your script hangs just interrupt it and start again. Podvisaniya stopped as soon as closed minicom.
Still the important point the majority of 3zh-modems under Linux is defined as 3 ustroystva:/dev/ttyUSB0, / dev/ttyUSB1, / dev/ttyUSB2. One is used for data transmission, another is similar for kakikhto service of commands of the modem, the third provides the standard COM interface for management. In a script it is necessary to specify the COM device of an interfes, in some modems it / dev/ttyUSB0, dev/ttyUSB2 is more rare/.

Depending on opportunities of the modem and a rate at the operator USSD requests and AT commands, it is possible to achieve sending sms, switching of the modes (2G/3G), to disconnect virtual sd-rum itd. Unfortunately for each model will have to select commands manually …

Unfortunately I a python practically do not know therefore most likely the script is written not in the most optimum way if ktoto undertakes to optimize it (and it is even better to issue all this in a graphic cover) I will be grateful and I will update a topic.

UPD Forgot to tell one more thing, the other day found kakogoto an opsos who provides Linux software to the modems. Modems naturally standard Chinese brendirovany under an opsos. Here for example ZTE MF100/MF62X is not obviously visible, but unfortunately I could force a softina to work with my modem. Can ktoto is more experienced will be able to dokolupat it? For its work libqt3-mt packet for architecture of i386 is necessary (otherwise it will not be started)

This article is a translation of the original post at
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here:

We believe that the knowledge, which is available at the most popular Russian IT blog, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.