📜 ⬆️ ⬇️

Megafon balance check script

Good day.

Once again I ran into the problem of checking the balance of my many MegaFon SIMs for the mobile Internet - a car, a summer house, an iPad, a mobile router, etc. The problem is that these SIM cards work in modems and other gadgets and may not always be at hand. Moreover, there is simply no time and desire to constantly monitor the state of balance in different devices, including remote ones. And the Internet usually has to “end” at the most inappropriate moment.

To date, the OPPOS has the following options for checking the balance:
1. Sending SMS to a short number
2. My online account
3. USSD team
4. Call to short number

I do not understand what prevents the operators from adding a module to their personal account - a notification by the subscriber’s email when the balance threshold is reached in xxx rubles.
Actually, that's what I'm going to implement with the bash script.
')
I'll go in a "dead-end" way - parsing the page of the personal account. Dead-end because OPPS constantly change something in the code of the pages and soon the script will stop working, like for example this one stopped working. In the near future I plan to make a script based on USSD or by sending an SMS, although in such cases you need to connect a modem to a computer or router (for example mikrotik), in which there is the possibility of creating scripts.

Here is a ready script for parsing the personal account page.

Briefly mechanism:
1. Saving the session ID and cookies with a curl (when entering the LC page with a certain script, the ID is generated and is attached to the current session, so the Curl will not log in).
2. Logging in LK using saved cookies and session ID.
3. Parsing the page and vytsepleniya balance.

#!/bin/bash cookies=./cookies.txt sessionID_url='https://lk.megafon.ru/login/' login_url='https://lk.megafon.ru/login/dologin/' logout_url='https://lk.megafon.ru/logout/' useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0" balance="" i=0 attempts=5 timeout=20 username='phone_number' # 10-     8-  7- password='password' #    while [[ "$balance" == "" && "$i" -le "$attempts" ]]; do let "i+=1" # ---------- CSRF / SessionID --------------- CSRF=`curl -A "$useragent" -c "$cookies" "$sessionID_url" | grep "CSRF_PARAM" | grep -o -E "([Aa]|[0-9])\S+-\S+-\S+-\S+-\S+([Aa]|[0-9])"` userdata="CSRF=$CSRF&j_username=$username&j_password=$password" balance=`curl -A "$useragent" -b "$cookies" -dump -L -X POST "$login_url" -d "$userdata" | grep -E -o "\s\S+\s[0-9]{1,},[0-9]{1,}" | grep -E -o "[0-9]{1,},[0-9]{1,}"` if [ "$balance" == "" ]; then sleep "$timeout" fi done if [ "$balance" != "" ]; then echo " = $balance ." else echo "   ,   ." fi # ------ LOGOUT --------------------------------- sleep 5 curl -b "$cookies" -L "$logout_url" > /dev/null 2>&1 if [ -f "$cookies" ]; then rm "$cookies" fi exit 0 


What are the "shoals":
1. Sometimes the mega server spits out “the service is temporarily unavailable”, so I made several attempts to get a balance with a timeout.
2. In case of login errors (wrong password or phone number) - a captcha may pop up and then the script will not work until you log in through the web with the captcha entered.
3. I don't like parsing in two passes with grep. I know that you can sed'om in one pass, but something is not coming out yet.
4. It is better to first save the entire code of the page to a temporary file, and then to parse ( as here ) - this will allow you to parry various errors of login or service.
5. Login and password is better to make the parameters passed to the script, to be more versatile.

Next, I will make a small control script (in the process of creation), which will call the above with various logins and passwords and check if the balance has fallen below a predetermined threshold with sending notifications on soap.
Next, I will stuff it into crones, for example, on a web server with the launch once a day.

Something like this.

Ideas and criticism are welcome.

ZY For private clients, it is better and easier to parse the IC (Internet Cabinet) page instead of the LK.
It allows you to log on to IR without a login, just go to the user.megafonmoscow.ru page via SIM card mega.
And there you can immediately strike off the balance and the rest of the traffic, which is also very important.
Today I am creating a new script.

The second version of the balance check script.
Will work only on tariffs for private owners. Checking the balance and the rest of the traffic is carried out by the SIM card with which the script goes to the Internet. The script sends a message to the soap in case of reaching a certain amount on the balance or a certain balance of traffic.

 #!/bin/bash page_dump=/usr/tmp/dump url='http://user.moscow.megafon.ru/' useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0" balance="" traffic="" phone="" balance_deadline="100" traffic_deadline="1" # -------- sendmail function ------------- function sendalert () { FROM_EMAIL_ADDRESS="my@mail.ru" EMAIL_USER="userlogin" EMAIL_ACCOUNT_PASSWORD="password" TO_EMAIL_ADDRESS="mailto@domain.ru" echo "$2" | mailx -s "$1" \ -S smtp-use-starttls \ -S ssl-verify=ignore \ -S smtp-auth=login \ -S smtp=smtp://smtp.yandex.ru \ -S smtp-auth-user="$EMAIL_USER" \ -S smtp-auth-password="$EMAIL_ACCOUNT_PASSWORD" \ -S from="$FROM_EMAIL_ADDRESS" \ "$TO_EMAIL_ADDRESS" } curl -A "$useragent" -dump "$url" >"$page_dump" balance=`cat "$page_dump" | grep -E -o 'balance(\S+\s)*' | grep -E -o '[0-9]{1,},[0-9]{1,}'` traffic=`cat "$page_dump" | grep -E -o 'traffic-by\S+' | grep -E -o '[0-9]{1,}(,[0-9]{1,})*'` phone=`cat "$page_dump" | grep -E -o 'phone.*+7' | grep -E -o '\([0-9]{3}\)\s[0-9]{3}(-[0-9]{2}){2}'` bal_test=`echo "$balance" | sed -e 's/,.*//'` traff_test=`echo "$traffic" | sed -e 's/,.*//'` if [ "$traff_test" -lt "$traffic_deadline" ]; then sendalert "   $phone" "  - $phone   - $traffic " fi if [ "$bal_test" -lt "$balance_deadline" ]; then sendalert "   $phone" "  - $phone   - $balance ." fi if [ -f "$page_dump" ]; then rm "$page_dump" fi exit 0 


The second option is the function of sending letters
I do not know which one is better. Works and so is quite stable.

 # -------- sendmail function ------------- function sendalert () { FROM_EMAIL_ADDRESS="my@mail.ru" EMAIL_USER="userlogin" SMTP_SERVER="smtp.yandex.ru" EMAIL_ACCOUNT_PASSWORD="password" TO_EMAIL_ADDRESS="mailto@domain.ru" sendEmail -f "$FROM_EMAIL_ADDRESS" \ -t "$TO_EMAIL_ADDRESS" \ -s "$SMTP_SERVER" \ -o message-charset=utf8 \ -xu "$EMAIL_USER" \ -xp "$EMAIL_ACCOUNT_PASSWORD" \ -u "$1" \ -m "$2" } 


The soap sending function was implemented in two ways - mailx and using the sendEmail perl pearl script. For the latter, you may need additional packages - Perl-Net-SMTP-SSL, Perl-Net-SSLeay, Perl-IO-Socket-SSL, Perl-Crypt-SSLeay.
The above mail sending scripts checked on smtp.yandex.ru !!! On other servers it may be necessary to tweak the settings.

ZZY
mailx is faster ...

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


All Articles