Send SMS from Linux CLI via Android Phone

in #utopian-io6 years ago (edited)

Hey everybody! Today I've got a task of mass sending a small SMS message to nearly 100 numbers.

Bulk

This task also included an important request for the option to be connected in CLI Pipe. What is CLI Pipe? It's a Linux option of connecting several programs to each other. Let's say, I have some source data with the numbers in Linux. I'm writing a program to find the numbers and send the messages in Linux, let's say Program 1 finds these numbers. I should be able to do like "./findnumbers | sendsmsmessages -t 'hello world'" and voila, it works. In the future, the other task might require sending messages too, like, I'll have ready to use data in MySQL database. I should be able, then, to print out the table with some MySQL request and still do "| sendsmsmessages -t 'sometext' " to send some SMS to these messages.

So what I am telling is that I need a flexible CLI instrument that can be easily linked to other programs in my OS. Develop clever once, save time in the future.

So I thought about options out there and sorted out this list (general, I'm listing all options!):

  1. There was a 'lazycrazy' and sort of strange way of manually entering all these numbers and sending the message from the phone, but it gonna take too much time and effort.
  2. Next, the mass-SMS sending programs for android. The problem is that they might be a crapware, spyware, and contain ads (if you know cool program of that sort please let me know in comments). So this option was declined like the first was.
  3. There is some desktop software that works in connection with phone software, like AirDroid, which allows you to send SMS messages. The problems of this way are: it still contain some ads, the program is closed source and we can't be sure it doesen't spy, and you can't easily connect SMS messaging option to any other program (like I was saying above with the "| sendsmsmessages" example)
  4. There is an option of creating a SSH session from phone, connect there and send messages. Problem is android phone currently doesen't allow to send messages even from local CLI manually. But there are "Termux Android API Plugins" that allows to control most of the phone functions via CLI and/or SSH.
  5. The last option is the hardest to develop but only this option allows to be safe & secure: developing own android application.

As idea #1 is time/effort taking, I'd have to take other options. Idea #2 is related to unknown software from unknown sources - it gonna take long time to find something free & good, so let's move on. Idea #3 is not so bad but lacks flexibility. Ideas #4 and #5 are good to go.

I'd started my work from the hardest option: trying to write own CLI android app to send messages. I've spent several hours on trying to find working example of SMS sending program (that works with the modern Android SDK) and I didn't find any. Another hard moment is that I wasn't going to install Android Studio - it's way too large and heavy for such a small app. Luckily, I found this Medium article but anyhow I've tried, googled for another articles etc. etc, the program never compiled. There was some errors with SmsManager include, while trying to fix it I found other several errors and I never managed to compile the program.

So I've returned to idea #4. I could say it's just 4 easy steps to start:

Single SMS send with Termux

  1. Install Termux itself from google play. Termux is android console interface with additional tools.
  2. Install Termux SDK plugins from google play. You will need them to send SMS messages, calls, and use other phone functions from CLI.
  3. Open Termux on the phone. Generate SSH certificate, transfer it to PC (by USB or via messenger) and add (just like to do with two servers or desktops, some info here )
  4. Check that PC and phone are on the same Wifi network. Check the IP of the phone.
  5. Still on the phone, in Termux, start ssh daemon in non-background mode (for debugging and easier work): sshd -d
  6. On the PC (Linux or other OS), connect to phone: ssh 192.168.1.100 -p 8022 - replace the IP with your phone IP address and port with your port (look at the Termux on the phone to see this info)
  7. That's it! You now can send messages like: termux-sms-send -n 0079044400030 hello from Termux. Read more about Termux API commands here. You could also call, search in contacts, run applications and do all other stuff of your phone - everything of these tasks now can be automated with the power of CLI!

Mass SMS send

But that was only one SMS! What we are trying to achieve, is sending a bunch of SMS messages to different numbers (and, if you like, with different text, too). As termux-sms-send is just a CLI program, we could easily make a Bash, python or any other language script which automates the task for us. I'm used to Bash so it's gonna be my example.

Let's make a script named mass-smssend.sh. It's the comfort of the PC (keyboard and all this stuff), but remember that we are working on the phone itself via SSH. So script will be physically located on the phone - don't forget to back it up to PC or cloud if you'll need it in the future.

  1. Make the script: nano mass-smssend.sh. It gonna open empty file with nano text editor.
  2. Copy-paste the following script:
#!/bin/bash
program="termux-sms-send"
number=( +79281234567 +79287654321 )
text="This is test SMS and now is $(date)"


for i in "${number[@]}"
 do
    (( x++ ))
    echo "Sending SMS number $x of ${#number[@]}"
    $program -n $i $text
    echo "Sent. Sleeping for 60 seconds to deliver the SMS..."
    sleep 60
 done

This small program is really easy to understand. What's happening here is:

  • We define three variables with program name (program), number array (number) and SMS message text (text).
  • Then we iternate over the array number, each time printing out current SMS number (beforehad adding 1 to x counter with x++), then sending a message and freezing program for 60 seconds to allow SMS to deliver (sleep 60).

You might change the delay seconds to less if your message is smaller or to larger number if you're sending some SMS poems. Other values, like SMS message text or any other information, can be easily changed in a similar manner.

Test result

I've tested the program on real array of 89 phone numbers, so it took 89 minutes to safely deliver these messages as they were quite large. Messages were delivered successfully.

The results are:

  • Spent few hours to understand that writing android app without android IDE is too hard and not so clever idea;
  • Spend one hour to understand how Termux works and 20 minutes more to write & debug the script.
  • Runned the script on real numbers for 89 minutes and noticed it works well. And I was free all these 89 minutes from routine work!

Totally spent: ~ 4 hrs 20 minutes
Totally saved in one sms send activity: ~ 1 hrs 29 minutes
Total sms send activities needed to make it affordable: 3~4 mass sendings

Link to Termux opensource app

So this month I'm going to start saving time by using my script. Hope it's useful for you, too.

Img

Feedback

Do you like the idea of automating a routine task? Did you ever tried to compile android app without IDE? Did you ever wrote a SMS messaing android app that work? If some of the answers are yes, please, write some details in comment section below.

Thanks and see you on the other side of the moon,
Den Ivanov aka @sxiii from Random City

Sort:  

Hi @sxiii, task requests are mostly meant for project owners who are seeking contributions to their project. This seems more like you are looking for someone to create something for you from scratch, but also seems like some sort of tutorial? I could be wrong, but at least this is how it feels to me.

I would recommend reading our guidelines so you can better understand what kind of contributions can be submitted to Utopian and in what category they belong.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @amosbastian thanks, should I put tag "development" in the place of "task-development"? I will re-review the guidelines to find if I misunderstood something.

There isn't an open source project linked in your post, so I'm confused as to whether you created one, or if it's just the mass-smssend.sh script. If you created one, then you should use the "development" tag and link it in the body of your post. As for the "task-development" tag; this is meant for project owners/maintainers looking for people who can solve a task (e.g. implementing a feature) for their open source project.

I would suggest you read this guide that was written by Utopian's current CTO. It describes how we review development contributions and what we like to see in them - it should provide some useful information!

Thank you very much for the links @amosbastian. I've updated the article to include the link to opensource "Termux" app. My script is also opensource, so it's two project: one large, the other small, in one.

Thank you for your review, @amosbastian! Keep up the good work!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 63626.14
ETH 3107.70
USDT 1.00
SBD 3.87