⬆️ ⬇️

Check the balance of the phone in the console

Each of us has a mobile phone, and someone has not even one. And there are telephones of next of kin. And if you check the balance on your phone is not difficult, then on the phone, which is not next to you it is difficult to do. Therefore, I had a desire to collect all my expenses on mobile communication in one place, so that they could be replenished in time, and see the history of operations. Yes, of course, you can attach a card to each phone and turn on auto-top up, but personally I don’t want to fly into an overdraft one day or be left without a phone, and this does not solve the problem of collecting statistics.



A couple of years ago, I used the “balance robot” program on an Android phone, which in the form of widgets showed the balance of my phones, but then I liked to constantly reflash my phone, and every time it was too lazy to restore the settings of the widgets, and I also wanted this information was not only available to me. And I decided that it is necessary to collect all this information on a web page.

I really like to work in the console, and I thought that I need to make two programs - one pulls out the balance of the phone, and the second collects statistics in the database and generates html pages with a report.



Honestly, it’s difficult to call a program, in fact it’s just a script that uses: curl, sed and awk to send requests to the operators ’web pages, and in the received answers it strikes the balance. The first version of the script pulled out only the balance, now the script has changed a lot and can handle errors that can return operator pages, for example, if the password is entered incorrectly. Also, the script is divided into functions, for each operator its own function, and service functions for processing the server's http response. Currently, the script can get a balance for: Megaphone, MTSa, Beeline, MGTSa, Onlime and Qiwi. The github source code is pbal . To use it, you need to clone it yourself and make pbal.sh executable, and run it with parameters - operator, phone, password:



$ ./pbal.sh mts 9164442211 123456 123.21 


pbal.sh

')

Inside the script, there is a timeout of work and the number of attempts to contact the server, they can be overridden by the script launch parameters. There is an option -v, which in addition to the balance shows the name of the operator and login, it is convenient if you run the script in a row several times with different operators. And the option –s, which in the case of errors does not show anything. The password can be omitted, then the script will request it at run time.



After that, when such a script was ready, collecting statistics was already quite simple. I wrote a script that, on a cron basis, runs pbal.sh once an hour and writes the resulting information to the database, and also generates static html pages. The script pbalweb.sh is in the web directory on githab. It is not necessary to use it at all; you can make your own, which will be, for example, use rrdtool to store the received information. But if it is interesting, the pbalweb.sh script creates a sqlite database along the path specified in the db variable and in the section "# Create some operators" writes op into the table — the operator type, login and password (This section is executed once the database is created, if necessary add a new operator, then you will need to do the INSERT manually). Further, if the database exists, then for each entry in the op table, pbal.sh is run and the received data is written to the bal table. After that, static html pages are generated in the directory specified in the webdir variable. These pages are generated on my web server, now I have access to the balance of my phones anywhere, as well as I see the history of expenses.



PS 01.11.2012 added by Kyivstar

Source: https://habr.com/ru/post/156581/



All Articles