How to use a free Web SMS from Sunrise within a shell script
Many mobile phone providers allow you a free amount of SMS if you use the web interface. Maybe you don’t want to log in every time to use them or you want to send the SMS from your own web site or even from within a shell script.
The following script can be uses to send free SMS with using the web SMS system of the Swiss Mobile provider Sunrise. It is adapted by the great Android tool from play.google.com/store/apps/details?id=de.ub0r.android.websms and especially the Sunrise plugin by Koni (play.google.com/store/apps/details?id=com.rothconsulting.android.websms.connector.sunrise). The source code for it is available under github.com/konir/websms-connector-sunrise.
I use it i.e. to check any incoming email and send me an SMS when ever a special subject is found.
Since I have two account at Sunrise, the commands are a bit more tricky as after the login the correct account has to be selected first. The good thing is that I can check the number of free SMS and switch to the other account once all SMS are used.
Updates
- 21.09.2014: Updated script with more options and corrected URLs
- 19.12.2014: Addapted URL changes for new Sunrise Webinterface, Extended script
Script
Send SMS
#!/bin/bash
##################################################################
# Script to send a SMS over the Sunrise free SMS webinterface
# Call it with no parameters to get usage help
# Copyright 2013-2014 by George Ruinelli <george@ruinelli.ch>
# Adapted from https://github.com/konir/websms-connector-sunrise
# Changelog
# 19.12.2014:
# - Fixed URL (http:// => https://)
# See changes on https://github.com/konir/websms-connector-sunrise/commit/bcbdba308b75c5c6b852bce147968a93a9668faa
# - Extended script to show numbe rof really used SMS after sending
##################################################################
#Example call:
# bash /home/ruinelli/sms/sms_send.sh -r +4176******* -m "This is an test message" -v -a
account1=076*******
account2=076*******
password="*******"
script_path=`dirname "$0"`
logfile="$script_path/send.log"
html_logfile="$script_path/sms_log.htm"
dryrun=0
verbose=0
showhelp=0
force_account1=0
extended=0
date >> $logfile
##########################
while getopts r:m:d?vax option #see http://linux.about.com/od/Bash_Scripting_Solutions/a/How-To-Pass-Arguments-To-A-Bash-Script.htm
do
case "${option}"
in
r) recipient=${OPTARG};;
m) message=${OPTARG};;
d) dryrun=1;;
v) verbose=1;;
a) force_account1=1;;
x) extended=1;;
?) showhelp=1;;
esac
done
if [ "$recipient" == "" ]; then
echo "ERROR: No recipient given"
showhelp=1
fi
if [ "$message" == "" ]; then
echo "ERROR: No message given"
showhelp=1
fi
if [ $showhelp -ge 1 ]; then
echo "---------------------------------------------------------------------------------"
echo "SMS sender sends a SMS over Sunrises Web2SMS interface"
echo "Since I have 2 accounts, it first uses the free SMS of the"
echo "2nd one, then switches to the 1st one."
echo "Copyright 2013 by George Ruinelli"
echo "Parameter:"
echo " -m MESSAGE (max 160 characters, more will be truncated)"
echo " -r RECIPIENT NUMBER (leading country code is optional)"
echo " -d (optional, dry run, does not actually send the message)"
echo " -v (optional, appends the number of available free SMS to the message)"
echo " -a (optional, force usage of account 2, even when no free SMS left)"
echo " -x (optional, split into multiple SMS if text is too long, max 480 characters)"
echo ""
echo "The message will be sent from account 2 ($account2) if there are free SMS left,"
echo "else account 1 ($account1) is used (unless -a is applied where always account 1 is used)"
echo "Example: bash /home/ruinelli/sms/sms_send.sh -v -r 076******* -m \"This is a test message\""
echo "---------------------------------------------------------------------------------"
exit
fi
if [ $verbose -ge 1 ]; then #make space for 9 characters additional: " (xx, xx)"
echo "Will append number of free SMS"
valid_message_lenght=151 #160-9
else
valid_message_lenght=160
fi
#check message length
length=`echo ${#message}`
if [ $extended -ge 1 ]; then
# nothing to do
if [ $length -gt $valid_message_lenght ]; then
echo "NOTE, message is too long ($length characters total), it might consume multiple SMS"
fi
else
if [ $length -gt $valid_message_lenght ]; then
let valid_message_lenght=valid_message_lenght-4
message=`echo ${message:0:$valid_message_lenght}`
message="$message..."
echo "WARNING, message is too long ($length characters total), will truncate it to: $message"
fi
fi
##########################
#logging in
echo "Logging in..." | tee -a $logfile
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d LoginForm_Login="$account1" -d LoginForm_Password="$password" -d LoginRedirectSecret="89cff646d5a3f65ce8a8ea59a3d7cad3" -d LoginRedirectURL="https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewStandardCatalog-Browse?CatalogCategoryID=zv7AqFI.Z.gAAAEkbGdQzDCf&LoginOrigin=Mein%20Konto&LoginDestination=Mein%20Konto" -d ajaxhref="http://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewPersonalCodeRetrieval-New?SpcRetrievalMode=ECare" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewApplication-Login > $html_logfile
echo "Selecting accounts and fetching left free SMS..." | tee -a $logfile
#getting amount of left free SMS for account 1
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account1" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > $html_logfile
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
left1=`grep "Gratis" $html_logfile`
left1=`echo $left1 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`
echo "Free SMS left on account 1: $left1" | tee -a $logfile
#getting amount of left free SMS for account 2
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account2" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > /dev/null
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
cat $html_logfile > "left_sms_account2.htm"
left2=`grep "Gratis" $html_logfile`
left2=`echo $left2 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`
# left2=0 #simulating no SMS left on account 2
echo "Free SMS left on account 2: $left2" | tee -a $logfile
### selecting account for sending
used_account1=""
used_account2=""
if [ $force_account1 -ge 1 ]; then #force account1
echo "Forcing to use account 1" | tee -a $logfile #if no free SMS left, we pay for them
account=$account1
# let left1=left1-1 #update verbose text for situation after sending
used_account1="*" #mark account
else
if [ $left2 -ge 1 ]; then #use account 2 first
echo "Using account 2" | tee -a $logfile
account=$account2
# let left2=left2-1 #update verbose text for situation after sending
used_account2="*" #mark account
else
echo "Using account 1" | tee -a $logfile #and if no free SMS left, we pay for them
account=$account1
# let left1=left1-1 #update verbose text for situation after sending
used_account1="*" #mark account
fi
fi
#now select right account
echo "Selecting sender: $account..." | tee -a $logfile
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > /dev/null
#add verbose text
if [ $verbose -ge 1 ]; then #make string showing free SMS: " (xx, xx)"
message="$message ($used_account1$left1, $used_account2$left2)"
fi
#sending
echo "" > $html_logfile #make sure no old status is used
echo "Sending message:" | tee -a $logfile
echo "<b>$message</b>" | tee -a $logfile
echo "to $recipient..." | tee -a $logfile
if [ $dryrun -ge 1 ]; then
echo "Dry-run, not sending it!"
else
echo "Sending..."
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d type="sms", -d recipient="$recipient" -d message="$message" -d send="send" -d task="send" -d currentMsisdn="$account" https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
fi
#status message
reply_line=`sed -n '/id="errorBlock"/=' $html_logfile`
let reply_line=reply_line+5
status=`head -$reply_line $html_logfile | tail -1`
status=`echo $status | sed 's/<br\/>//g'`
echo "---------------------------------------------------------"
echo "Status: $status" | tee -a $logfile
echo "---------------------------------------------------------" | tee -a $logfile
# echo "Selecting accounts and fetching left free SMS..." | tee -a $logfile
#getting amount of left free SMS for account 1
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account1" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > $html_logfile
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
left1b=`grep "Gratis" $html_logfile`
left1b=`echo $left1b | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`
# echo "Free SMS left on account 1: $left1b" | tee -a $logfile
#getting amount of left free SMS for account 2
echo "" > $html_logfile #make sure no old status is used
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account2" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > /dev/null
/usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile
cat $html_logfile > "left_sms_account2.htm"
left2b=`grep "Gratis" $html_logfile`
left2b=`echo $left2b | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '`
# echo "Free SMS left on account 2: $left2b" | tee -a $logfile
let "used1=left1-left1b"
let "used2=left2-left2b"
# echo "$left1, $left1b, $used1"
# echo "$left2, $left2b, $used2"
# echo "---------------------------------------------------------"
echo "Used SMS on account 1: $used1" | tee -a $logfile
echo "Used SMS on account 2: $used2" | tee -a $logfile
echo "---------------------------------------------------------"
Check left SMS
#!/bin/bash account1=076****** account2=076****** password="********" script_path=`dirname "$0"` logfile="$script_path/send.log" html_logfile="$script_path/sms_log.htm" #logging in # echo "Logging in..." | tee -a $logfile /usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d LoginForm_Login="$account1" -d LoginForm_Password="$password" -d LoginRedirectSecret="89cff646d5a3f65ce8a8ea59a3d7cad3" -d LoginRedirectURL="https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewStandardCatalog-Browse?CatalogCategoryID=zv7AqFI.Z.gAAAEkbGdQzDCf&LoginOrigin=Mein%20Konto&LoginDestination=Mein%20Konto" -d ajaxhref="http://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewPersonalCodeRetrieval-New?SpcRetrievalMode=ECare" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewApplication-Login > $html_logfile # echo "Selecting accounts and fetching left free SMS..." | tee -a $logfile #getting amount of left free SMS for account 1 echo "" > $html_logfile #make sure no old status is used /usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account1" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > $html_logfile /usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile left1=`grep "Gratis" $html_logfile` left1=`echo $left1 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '` echo $left1 #getting amount of left free SMS for account 2 echo "" > $html_logfile #make sure no old status is used /usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 -d charsLeft="0" -d PhoneNumber="$account2" https://www1.sunrise.ch/is-bin/INTERSHOP.enfinity/WFS/Sunrise-Residential-Site/de_CH/-/CHF/ViewECareMessaging-Encrypt > /dev/null /usr/local/bin/curl -s --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 https://mip.sunrise.ch/mip/dyn/login/smsMeinKonto?lang=de > $html_logfile cat $html_logfile > "left_sms_account2.htm" left2=`grep "Gratis" $html_logfile` left2=`echo $left2 | sed 's/Gratis//g'| tr -d '\n' | tr -d '\r' | tr -d ' '` # left2=0 #simulating no SMS left on account 2 echo $left2

Danke. Schade, dass sie kein richtiges API anbieten. Ich werde mir Telegram auch mal anschauen, die App hab ich ja schon… liebe Grüsse, Dani
PS: Wie analysierst du die ablaufenden http-Requests (bei Sunrise)? Ich würde die Sache trotzdem gerne mal anschauen.
Habs bis jetzt nur mal mit den Developer-Tools von Firefox/Chrome probiert, aber wie gesagt, ohne Erfolg.
Meine Motivation war aber auch nicht sehr gross und dementsprechend habe ich nicht viel Zeit investiert.
Zudem bin ich mittlerweile nicht mehr bei Sunrise.
Ist es mit den Developer Tools denn möglich aufzuzeichnen, welche Requests der Reihe nach empfangen oder verschickt werden?
Wenn du nicht mehr bei Sunrise bist, ist die Motivation natürlich nicht mehr so gross, in der Tat. Ich bin auch nicht mehr bei denen, sondern bei UPC, habe einfach noch einige SIM-Karten von Sunrise, die sich für gratis SMS anzubieten schienen.
Ja, mit den Developer Tools solltest Du alles sehen, auch die Cookies und Sessions.
Man muss sich aber halt schon recht tief in die Thematik (AJAX, Sessions, …) einarbeiten, um es nachvollziehen zu können.
Einfacher könnte es sein, die Änderungen von Koni auf Github zu studieren (Sofern er es gefixt hat).
Hi George
Bis vor unlang hat dein Skript bei mir wunderbar funktioniert. Nächste Woche wollte ich es wieder mit meinen Schülern im Freifach IoT anschauen – nur scheint es nicht mehr zu funktionieren. Hast du möglicherweise für dich schon eine geupdatete Version erstellt, die mit der geänderten (?) Sunrise-Homepage (wieder) klar kommt? Ich hätte grosses Interesse daran.
Danke dir und liebe Grüsse,
Dani
Hoi Dani
Leider hat Sunrise schon länger (wieder) einmal etwas an der Webseite geändert. Ich habs leider nicht mehr zum Laufen gekriegt. Ich hatte auch mal Koni kontaktiert, aber leider nie mehr etwas von ihm gehört. Auch der von ihm gewartete Android WebSMS-Connector geht nicht mehr gemäss den Kommentaren auf https://play.google.com/store/apps/details?id=com.rothconsulting.android.websms.connector.sunrise .
Ich selber bin mitlerweise auf Telegram gewechselt und habe mir einen Bot geschrieben, dem kann ich nun via URL-Aufruf sagen, er solle mir/jemandem eine Nachricht schreiben. Das Ganze ist gratis und unlimitiert. Nur muss man halt Telegram haben und eine Internetverbindung.