Wednesday, May 16, 2018

Update: Getting Status Data from a Huawei E8372 USB Dongle

In my previous post, I describe a script for getting the status from a Huawei dongle. Since I wrote that, I have improved the script that I wrote. Here is the updated script:

#!/bin/bash

#Status: http://hi.link/api/monitoring/status (GET)
#    ConnectionStatus:
#        112 = no autoconnect
#        113 = no autoconnect on roaming
#        114 = no reconnect
#        115 = no reconnect on roaming
#        900 = connecting
#        901 = connected
#        902 = disconnected
#        903 = disconnecting
#
#                   |    discon      |      con      |
#                   |   2G   |   3G  |   2G  |   3G  |
#      NetworkType: |   3    |   4   |   3   |  4/7  |
#      3 = 2G / 4 = 3G (UMTS) / 7 = 3G+ (HSDPA)
#
#    SignalStrength: strength in percent
#
#    ServiceStatus: 1 = enter PIN / 2 = PIN correct
#    SimStatus: 0 = no SIM access / 1 = SIM acc

site='192.168.8.1'

element_ses='response/SesInfo'
element_tok='response/TokInfo'
element_connstat='response/ConnectionStatus'
element_networktype='response/CurrentNetworkType'
element_sigicon='response/SignalIcon'
element_wanip='response/WanIPAddress'

url="http://${site}/api/webserver/SesTokInfo"

xml=$(curl -k --silent -X GET "$url")

#echo "$xml"

COOKIE=$(printf '%s\n' "$xml" | xmlstarlet sel -t -v "$element_ses")
TOKEN=$(printf '%s\n' "$xml" | xmlstarlet sel -t -v "$element_tok")

#echo "$COOKIE"
#echo "$TOKEN"

monit_url="http://${site}/api/monitoring/status"
device_url="http://${site}/api/device/basic_information"


MODEMSTATE=$(curl -k --silent -X GET "$monit_url" -H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" -H "Content-Type: text/xml")
#curl -k --silent -X GET "$url" -H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" -H "Content-Type: text/xml"

#echo "$MODEMSTATE"

CONNSTAT=$(printf '%s\n' "$MODEMSTATE" | xmlstarlet sel -t -v "$element_connstat")
NETWORKTYPE=$(printf '%s\n' "$MODEMSTATE" | xmlstarlet sel -t -v "$element_networktype")
WANIP=$(printf '%s\n' "$MODEMSTATE" | xmlstarlet sel -t -v "$element_wanip")
SIGICON=$(printf '%s\n' "$MODEMSTATE" | xmlstarlet sel -t -v "$element_sigicon")

echo "Connection State: $CONNSTAT"
echo "    Network Type: $NETWORKTYPE"
echo "     Signal Icon: $SIGICON"
echo "          WAN IP: $WANIP" 

This script works better than the old one and lets you return just the information you want. Note, in my script I return the signal strength icon because the Signal Strength value seems to always be null for this dongle. You can tell relative strength by the signal strength icon. Here is some sample output. I have since gotten a dongle that is compatible with T-Mobile networks in the USA, so the network type is 19 - I guess that means LTE since that is what the dongle's website says when I connect to it and look at the network type. Note, I replaced my IP with x's

pi@remotepi:~/modemwork $ ./get-modem-state
Connection State: 901
    Network Type: 19
     Signal Icon: 1
          WAN IP: xx.xxx.xxx.xxx 

In order to really interact with the dongle, I needed to be able to login to the web site that the dongle provides. To do that, I had to move to using a library that was written by someone else. I will make a new blog post to describe how I use it to do more with the dongle, including getting and sending SMS messages!

2 comments:

  1. Thanks for your great work. It helped me to remotely monitoring multiple E8372 for my company in remote locations.

    I added some lines to display the connection statistics since we are using top-up SIMs and needs to know when to top-up the SIMs.

    element_currcontime='response/CurrentConnectTime'
    element_currupload='response/CurrentUpload'
    element_currdownload='response/CurrentDownload'
    element_totalcontime='response/TotalConnectTime'
    element_totalupload='response/TotalUpload'
    element_totaldownload='response/TotalDownload'

    traffic_url="http://${site}/api/monitoring/traffic-statistics"

    CONNECTIONSTAT=$(curl -k --silent -X GET "$traffic_url" -H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" -H "Content-Type: text/xml")

    CCONNECTTIME=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_currcontime")
    CUPLOAD=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_currupload")
    CDOWNLOAD=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_currdownload")
    TCONNECTTIME=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_totalcontime")
    TUPLOAD=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_totalupload")
    TDOWNLOAD=$(printf '%s\n' "$CONNECTIONSTAT" | xmlstarlet sel -t -v "$element_totaldownload")

    eval "echo Current Connection Time: $(date -ud "@$CCONNECTTIME" +'$((%s/3600/24)) days %H hours %M minutes %S seconds')"
    echo " Current Upload: $(/usr/bin/awk "BEGIN {print $CUPLOAD/1048576}") MB"
    echo " Current Download: $(/usr/bin/awk "BEGIN {print $CDOWNLOAD/1048576}") MB"
    echo " Current Data Usage: $(/usr/bin/awk "BEGIN {print ($CUPLOAD+$CDOWNLOAD)/1048576}") MB"
    eval "echo \ \ Total Connection Time: $(date -ud "@$TCONNECTTIME" +'$((%s/3600/24)) days %H hours %M minutes %S seconds')"
    echo " Total Upload: $(/usr/bin/awk "BEGIN {print $TUPLOAD/1048576}") MB"
    echo " Total Download: $(/usr/bin/awk "BEGIN {print $TDOWNLOAD/1048576}") MB"
    echo " Total Data Usage: $(/usr/bin/awk "BEGIN {print ($TUPLOAD+$TDOWNLOAD)/1048576}") MB"

    ReplyDelete
    Replies
    1. Beautiful, I am glad it helped! Checkout the other blog post because in that you can actually send an SMS message - you could use that to have the dongle call home when it is getting low. Good luck!

      Delete