Script check balance Megaphone

    Good day.

    Once again, I encountered the problem of checking the balance of my numerous Megafon SIM cards for mobile Internet - a car, a summer residence, 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 an "end" at the most inopportune moment.

    To date, the OPPS has the following options for checking the balance:
    1. Sending SMS to a short number
    2. Personal Internet account
    3. USSD team
    4. Calling a short number

    I do not understand what prevents operators from adding a module to their personal account - email notification the subscriber when the balance threshold is reached in xxx rubles.
    Actually, this is what I am going to implement using a bash script.

    I’ll go the “dead end” way - parsing the page of my personal account. Deadlock because OPSOsy constantly change something in the code pages, and soon the script will no longer work as such has ceased to work here this. In the near future I plan to make a script based on USSD or by sending SMS, although in such cases it is necessary to connect the modem to a computer or router (for example mikrotik), in which it is possible to create scripts.

    Here is a ready-made script for parsing your personal account page.

    Briefly, the mechanism:
    1. Saving the session ID and cookies by the Kuril (when entering the LC page, an ID is generated by a script and attached to the current session, so you will not be logged in with one Kuril call).
    2. Logging in to the user account using stored cookies and session ID.
    3. Parsing the page and grabbing the 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 “jambs”:
    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. For login errors (incorrect password or phone number) - a captcha may pop up and then the script will not work until you log in through the web with captcha input.
    3. I do not like parsing in two passes grep'om. I know that you can sed'om in one pass, but something does not work out yet.
    4. It is better to first save the entire code of the page to a temporary file, and then parse it ( as here ) - this way it will be possible to parse various login or service errors.
    5. It is better to put the login and password into the parameters passed to the script, so that it is more universal.

    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 whether the balance has fallen below a given threshold with sending notifications by soap.
    Then I will push it into crowns, for example, on a web server with launching once a day.

    Something like this.

    Ideas and criticism are welcome.

    Z.Y. For private clients, it’s better and easier to parse the IR (Internet Cabinet) page instead of LK.
    Starts up on IR without logging in, just go to user.megafonmoscow.ru page via SIM card mega.
    And you can parse there immediately and the balance and the rest of the traffic, which is also very important.
    Today I'm creating a new script.

    The second version of the balance check script.
    Will work only on tariffs for private owners. Checking the balance and balance of 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 sheet 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 of the function of sending letters
    I do not know which is better. It works quite stably anyway.

    # -------- 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"
    }
    


    Function to send soap sold in two ways - the mailx and using perl script sendEmail . For the latter, additional packages may be required - Perl-Net-SMTP-SSL, Perl-Net-SSLeay, Perl-IO-Socket-SSL, Perl-Crypt-SSLeay.
    I checked the above scripts for sending mail to smtp.yandex.ru !!! On other servers, it may be necessary to tweak the settings.

    Z.Z.Y.
    mailx is faster ...

    Also popular now: