How To Run Commands on Raspberry Pi by Email
This problem is faced by nearly every Raspberry Pi user. You have a Raspberry Pi running at home, and want to execute a command on it. The issue is, you are outside your home, on your mobile network, and have no way to access your Pi. You can get a static IP address, or use one of the dynamic DNS services to enable access to your home network. However, that is just too inconvenient for most people and takes too much effort for little gain. In addition, it opens your Raspberry Pi to brute-force attacks, since the SSH port is now exposed to the world.
The good news is, there is a more convenient, and more secure method –simply email the commands you wish to run on your Raspberry Pi. This way, all you need is an email client and a working internet connection on your Pi. No logins, and no static IP’s. What’s more, this method not only works with the Raspberry Pi, but with any Linux distro with the Bash shell and cURL installed. Sounds cool? Here is what you need to set it up:
Requirements
Getting Started
Set Up IFTTT Recipe
We will use IFTTT to distribute commands delivered by e-mail to Dropbox. After setting up an IFTTT account, you will have to use the “Run bash commands on Raspberry Pi by e-mail” recipe. Simply, go to thisIFTTT recipe, and click onAdd.
Set Up Dropbox Uploader
We needDropbox Uploader–a command-line program that can upload, download, list or delete files from Dropbox. Here is how to set it up :
1.On your Raspberry Pi,get the prerequisite softwarecURLandwget. You probably already have these installed, but we will install them just in case:
[sourcecode]sudo apt install wget curl[/sourcecode]
2.Create a folderwhere we will set it up. Here, I’ve created a directory calledraspi_emailin my home folder:
[sourcecode]mkdir ~/raspi_emailcd ~/raspi_email[/sourcecode]
3.Get theDropbox Uploader script from GitHub, andmark it executable:
[sourcecode]wget https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh
chmod +x ./dropbox_uploader.sh[/sourcecode]
- Runthedropbox_uploader.shscript. You will see an output like this:
5.Now you need tocreate a Dropbox app. Log in to Dropbox, go tothis URL, where you will see a screen that looks like this:
6.As in the image above, first choose theDropbox API, then selectFull Dropboxin the type of access needed. Finally,Nameyour app whatever you like.
7.After your new app is successfully created, scroll down to theOAuth2section, and click on theGeneratebutton to generate an access token. You will now see a generated access token where theGeneratebutton was. Copy this token, paste it in your terminal, and pressEnter:
8.Toverify that your account is working, execute the command./dropbox_uploader.sh list– you should see a list of the files in your Dropbox account.
Set Up Raspi Runner
On your Raspberry Pi, download theRaspi Runner scriptin theraspi_emailfolder we created previously:
[sourcecode] cd ~/raspi_email
wget https://raw.github.com/enkydu/Raspi_Runner/master/raspi_runner.sh[/sourcecode]
Make the scriptexecutable, and run it:
[sourcecode]chmod +x ./raspi_runner.sh
./raspi_runner.sh[/sourcecode]
You will now see a prompt like the image below. When the script asks for the full path to yourDropbox Uploader,enter the pathto yourHomedirectory (probably/home/pi/), followed by thename of the folderin which you downloaded thedropbox_uploader.shscript. If you followed this guide word by word, you will enter/home/pi/raspi_emailhere.
Set Up Crontab
Once the basics are in place, it is necessary toschedule the running of Raspi Runner. For example, you can set it to check for emails every 5 minutes.We will usecrontabfor this. On your Raspberry Pi, run:
[sourcecode]crontab -e[/sourcecode]
On the bottom of the file, add the following line:
[sourcecode]*/5 * * * * /home/pi/raspi_email/raspi_runner.sh > /dev/null 2>&1[/sourcecode]
Of course, in the above example,replacepiwith your username, andraspi_emailwith the folder you placed theraspi_runner.shscript in. Now save your crontab.
Usage
Simply send an email with the commands you wish to execute on your Pi totrigger@recipe.ifttt.comfrom theemail address you used to set up your IFTTT account. The body of the email will contain the command. You can add whatever subject you like to the email, but it is recommended that you do add one. In the following example, I am commanding my Raspberry Pi toshut down:
After the amount of delay you set in yourcrontab, theraspi_runner.shwill run and execute the command contained in your email.
SEE ALSO:How to Set Up and Get Started with Raspberry Pi 3 (Starter Guide)
Run Commands Remotely On Your Raspberry Pi Using IFTTT
So this is how you can use IFTTT, Dropbox, and some command line magic to run commands remotely on your Raspberry Pi. Of course, as mentioned in the beginning, this method will also work with any Linux or Unix-like system with the Bash shell installed.
Stay tuned for more Raspberry Pi tips and tricks in the future. If you have trouble setting it up, or want to share some of your own cool IFTTT tricks with us, make sure to drop us a line in the comments section below.
Shivam Raj
Command lines, DIY, video games, linguistics, and a little bit of everything else.
Add new comment
Name
Email ID
Δ
01