Difference between revisions of "Telenor SMS"

From ivc wiki
Jump to navigationJump to search
Line 79: Line 79:
== References ==
== References ==
* [http://www.telenor.no/mobil/ Telenor Mobil]
* [http://www.telenor.no/mobil/ Telenor Mobil]
* [http://osr507doc.sco.com/en/OSUserG/_Passing_to_shell_script.html Passing arguments to a shell script]
* [http://www.fischerlaender.net/webdev/redirecting-wget-to-stdout Redirecting wget to STDOUT]
* [http://www.fischerlaender.net/webdev/redirecting-wget-to-stdout Redirecting wget to STDOUT]
* [http://www.allinterview.com/showanswers/13077.html awk - Two strings, extract part]
* [http://www.allinterview.com/showanswers/13077.html awk - Two strings, extract part]
* [http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides dirname - Find out which directory the script file resides]
* [http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides dirname - Find out which directory the script file resides]

Revision as of 23:18, 3 February 2009

As a Telenor mobile subscriber, you have the possiblity to send SMS messages via their website. Also, for as long as I can remember, the first 30 messages each month are free.

This is a simple script I made to simplify the process. Instead of having to log-in to post a message, why not do it from the command-line.

Shell Script

The shell script utilizes wget for the web-requests and has some sanitation, error, and debugging functionality. Please feel free to alter the script and give me suggestions for improvements.

#!/bin/sh
#
# Telenor Send SMS Script - by ivc
#
# 2009-02-03: Initial version
#

# Settings
username=<your telenor cellnumber>
password=<account password>
debug=false


# Rest of the script, no editing needed
tonumber=$1
message=`echo $@|sed -e 's/'"$tonumber"' //g' -e 's/ /%20/g'`
cwd=`dirname $0`
cookiesfile=$cwd/telenor_cookies.txt

echo -e "\n Telenor Send SMS Shell Script - by ivc\n"

# Some sanitation
if [ -z `which wget` ]; then echo -e "  wget binary is missing or is not in the path, install wget and get back to the script.\n"; exit; fi;
if [ -z $1 ] || [ -z $2 ]; then echo -e "  Missing arguments!\n\n  Usage: $0 <number> <text message>\n  E.g. $0 99933322 Hi m8, bouldering today?\n"; exit; fi

# Debug mesages and setup
if [ $debug == true ]; then echo "  Sending message \"$message\" to number $tonumber"; fi


# Login
echo -e "  Logging in..."
login=`wget --quiet -O- --save-cookies=$cookiesfile --keep-session-cookies \
--post-data="j_username=$username&j_password=$password&fromweb=undefined" https://telenormobil.no/minesider/login.do`

if [ $debug == true ]; then echo "$login"; fi

loginsuccessful=`echo $login|grep "$username"`
toomanyattempt=`echo $login|grep "sperret"`

if [ -n "$loginsuccessful" ]; then echo -e "  Successfully logged in.";
elif [ -n "$toomanyattempt" ]; then echo -e "  Server reports too many failed attempts, try again in 30 minutes.\n"; exit;
else echo -e "  Failed to log in, check the username and password!\n"; exit; fi


# Send SMS
echo -e "  Sending message..."
sendsms=`wget --quiet -O- --load-cookies=$cookiesfile --keep-session-cookies \
--post-data="toAddress=$tonumber&message=$message&b_send=Send" https://telenormobil.no/ums/compose/sms/process.do`

if [ $debug == true ]; then echo "$sendsms"; fi

sendsmssuccessful=`echo $sendsms|grep "Meldingen er sendt"`

if [ -n "$sendsmssuccessful" ]; then echo -e "  Message sent!\n"; else echo -e "  Failed to send message, try again later.\n"; exit; fi


# SMS left count
smsleft=`wget --quiet -O- --load-cookies=$cookiesfile --keep-session-cookies https://telenormobil.no/ums/compose/sms.do`

if [ $debug == true ]; then echo "$smsleft"; fi

smsleftcount=`echo $smsleft|awk -F"web: " '{print $2}'|sed 's/<br\/>/ /g'|cut -d" " -f1`

if [ -n "$smsleftcount" ]; then echo -e "\n  Number of free web SMS messages left: $smsleftcount\n"; fi


# Delete cookies file
rm -f "$cookiefile"

Save it as telenor_send_sms.sh and make it executable, chmod +x telenor_send_sms.sh.

References