Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Last active August 29, 2018 04:55
Show Gist options
  • Select an option

  • Save heisvoid/22b9d46d727b254409bc351fac118d86 to your computer and use it in GitHub Desktop.

Select an option

Save heisvoid/22b9d46d727b254409bc351fac118d86 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Check usage for AMobile, a.k.a. AnnexTelecom.
show_usage ()
{
local name=`basename "$0"`
cat << EOF
Usage: $name [OPTIONS] -i ID -p PASSWORD
Description:
Check cell phone usage for AMobile, a.k.a. AnnexTelecom.
-i
Assign the user ID.
-c
Assign the user password.
-h
Show usage.
EOF
}
send_mail ()
{
local text="$1"
echo "$text" | mailx -A gmail -s "$text" "[email protected]" >/dev/null 2>&1
}
handle_error ()
{
local msg="$1"
echo "$msg" >&2
echo ""
send_mail "$msg"
}
id=""
pw=""
while getopts ":i:p:" option; do
case "$option" in
i)
id="$OPTARG"
;;
p)
pw="$OPTARG"
;;
*)
show_usage
exit 1
;;
esac
done
if [ -z "$id" -o -z "$pw" ]; then
echo "Error: The user ID and password should be specified." >&2
echo ""
show_usage
exit 1
fi
COOKIE=`mktemp`
if [ 0 -ne $? ]; then
handle_error "Error: Failed to create a temporary file for the cookie."
exit 1
fi
# http://archive.is/zGaCe
# http://archive.is/mRvpy
# http://archive.is/yDvBx
curl -s -f -X POST "http://annextele.com/csmenu/login_act.asp" -d "userid=$id&userpw=$pw" -c "$COOKIE" -o /dev/null
if [ 0 -ne $? ]; then
handle_error "Error: Failed to login: $id"
exit 1
fi
HTML=`mktemp`
if [ 0 -ne $? ]; then
handle_error "Error: Failed to create a temporary file for the html."
exit 1
fi
curl -s -f "http://annextele.com" -b "$COOKIE" -o "$HTML"
if [ 0 -ne $? ]; then
handle_error "Error: Failed to get the html file."
exit 1
fi
DATA=`mktemp`
if [ 0 -ne $? ]; then
handle_error "Error: Failed to create a temporary file for the data."
exit 1
fi
# http://archive.is/DixUD
xmllint --html --xpath '//li[@class="data"]/dl/dt/text()' "$HTML" > "$DATA" 2> /dev/null
if [ 0 -ne $? ]; then
handle_error "Error: Failed to get data usage."
exit 1
fi
tmp=`cat "$DATA" | cut -d" " -f1`
# Conversion from FLOAT to INT
# http://archive.is/a8GBU
# http://archive.is/nXlOA
# http://archive.is/BOF1X
d=${tmp%.*}
if [ 100 -gt $d ]; then
HISTORY="$HOME/.amobile-$id"
last=`cat "$HISTORY" 2>/dev/null`
now=`TZ="Asia/Seoul" date +"%Y%m" 2>/dev/null`
if [ "$last" != "$now" ]; then
send_mail "WARN cell data $d MB remains: $id"
echo "$now" > "$HISTORY"
fi
fi
rm "$COOKIE" "$HTML" "$DATA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment