If you want to send SMS via playSMS using bash script or CLI, use the WEB SERVICES options in playSMS to facilitate this function. Its very useful in many scenarios.
Example:
I wanted to schedule a bash script which should run daily twice , this script collects various information of different servers, routers and then summarize it and send to admin via SMS. I could do it with GAMMU or other sms tools too, but the issue was that the server already have KANNEL with gsm modem configured and (in general standard) Kannel dont accept message with special characters, spaced, and strange formats that this script output, (with urlencoding possible but i dont want to do it) or I had to use sed awk and other tools to proper format them which was overhead work. and I wanted to utilized KANNEL or playSMS.
Mr. Anton from playSMS suggested to use web-services of playSMS and it worked beautifully :)
This is how it was done.
1- Enable WEB SERVICES & GENERATE TOKEN
Goto My Account > User Configuration
Select YES from drop down menu for “Enable webservices” and “Renew webservices token“
Click on SAVE.
As showed in the following image below …
Now on same page you will now see the TOKEN number as showed in the following image below …
Copy / note down this this number, It will serve as a kind of password to let us send sms via php script.
2- Create & schedule PHP script to execute your shell script
Now we have to create a PHP script which will use the above TOKEN to let us run the bash script and send it’s output to admin mobile number :)
mkdir /temp touch /temp/dailysms.php chmod +x /temp/dailysms.php nano /temp/dailysms.php
now paste the following date , but be sure to modify the token number as per your own setup, and the script which you want to execute via php.
#!/usr/bin/php -q # Script provided by mr.anton # https://forum.playsms.org/t/schedule-message-to-run-sms-command/194/2 # https://aacable.wordpress.com # Dated: 30th July, 2015 # Syed Jahanzaib <?php /** * cli2sms.php by Anton Raharja (antonrd@gmail.com) * Example script to get data from shell script and send it as SMS via playSMS * You need to have a configured and working playSMS * In this example playSMS is accessible from http://localhost/playsms * Don't forget to chmod +x cli2sms.php to use it from Linux shell * * You may modify this script to suit your needs * * Example usage: * - get stat data (eg: uptime) and send it periodically (using cron) to admin's mobile phones * - https://aacable.wordpress.com/2015/07/30/playsms-send-sms-via-scriptcli-using-webservices-token/ */ // suppress error message error_reporting(0); // playSMS username/account for sending SMS $username = 'admin'; // Webservices token for above username $token = '1194df9e20d06c3790f0c6fef49f174a'; // playSMS Webservices URL $playsms_ws = 'http://localhost/playsms/index.php?app=ws'; // destination numbers, comma seperated or use #groupcode for sending to group // $destinations = '#devteam,+6200123123123,+6200456456456'; // $destinations = '+6200123123123,+6200456456456'; # for multiple recipients</pre> <pre>$destinations = '03333021909';</pre> <pre> // get message to send from another shell script or Linux command, for example 'uptime' // $message = trim(shell_exec('uptime')); $message = trim(shell_exec('/temp/dailysms.sh'));</pre> <pre> // send via playSMS HTTP API if ($message) { $ws = $playsms_ws . '&u=' . $username . '&h=' . $token . '&op=pv'; $ws .= '&to=' . urlencode($destinations) . '&msg='.urlencode($message) . '&nofooter=1'; $ret = @file_get_contents($ws); // echo $ret; echo "OK: message sent" . PHP_EOL; } else { echo "ERROR: message is empty" . PHP_EOL; } // end of script
TEST
Now test by running the php file we just created above.
/temp/dailysms.php
and you should see something like below if every thing goes smoothly as planned
root@radius:/temp# ./dailysms.php OK: message sent
and you will soon receive the SMS on your Mobile.
Regard’s
Syed Jahanzaib
Filed under: Linux Related