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.

    useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
    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])"`
    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"
    if [ "$balance" != "" ]; then
       echo "Баланс = $balance руб."
          echo "Не удалось проверить баланс, повторите попытку позже."
    # ------ LOGOUT ---------------------------------
    sleep 5
    curl -b "$cookies" -L "$logout_url" > /dev/null 2>&1
       if [ -f "$cookies" ]; then 
          rm "$cookies"
    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 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.

    useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
    # -------- sendmail function -------------
    function sendalert () {
    echo "$2" | mailx -s "$1" \
    -S smtp-use-starttls \
    -S ssl-verify=ignore \
    -S smtp-auth=login \
    -S smtp=smtp:// \
    -S smtp-auth-user="$EMAIL_USER" \
    -S smtp-auth-password="$EMAIL_ACCOUNT_PASSWORD" \
    -S from="$FROM_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 ГБ" 
    if [ "$bal_test" -lt "$balance_deadline" ]; then
       sendalert "Баланс ниже допустимого $phone" "Номер телефона - $phone
    Ваш баланс - $balance руб." 
    if [ -f "$page_dump" ]; then 
       rm "$page_dump"
    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 () {
    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 !!! On other servers, it may be necessary to tweak the settings.

    mailx is faster ...

    Also popular now: