Simple Remote Control with Dropbox

Dropbox

Dropbox is a fantastic application for unobstructive synchronization of files between multiple computers running different operating systems. Everything happens seamlessly behind the scene. The more I use it the more I feel thankful to the creators and devs. Today I found out a new use of Dropbox — a simple remote control tool. All you need is Dropbox and some shell scripts.

The Scenario

Why I would have come up with such an idea? The story is: I have a PC at home which I would like to access from the lab in the institute. It is reachable only if it’s connected to the VPN network in the campus. However, the VPN connection is sometimes not very stable and could die with no reason, not to say the server will automatically terminate a connection from outside of the university every 4 hours.

Therefore I really want to find a way to remotely issue commands on my home PC (which is unreachable), let it connect to the VPN network and report the IP address to me. With Dropbox, its very easy to do.

The Solution

I create a cron job on the home PC, which checks a text file (command.txt) inside my Dropbox folder every minute, when its not empty, execute the content of the file (and clear the file). On the lab PC, I can simply put the command I want to execute into the same file — since its synchronized seconds after I made the modification, it will soon be executed at home. And for reporting IP address, I wrote a wrapper script which runs on home PC to check the VPN status after connecting and redirect the IP address information into another text file (ip.txt) inside Dropbox folder. So when I am in the lab, I put the VPN wrapper script’s name inside the command.txt, maximum a minute later, I check the ip.txt. Now I can ssh to my home PC and do what ever I want.

The Script

I put up together a script which does both the adding command and the checking-then-running command job. I call it “dbrun” which is short for “Dropbox Run”. It can be download here (right-click and save).

Installation & Usage:

  1. Make sure Dropbox daemon is up and running in both systems.
  2. Put dbrun in both the controlling and controlled systems. I prefer the directory ~/.local/bin.
  3. Make it executable if it’s not. chmod +x dbrun
  4. Create a cron job to run dbrun in a short interval (e.g. 1 min) in the controlled system. I do this by creating a file crontab inside ~/.local/etc and then run crontab ~/.local/etc/crontab. The content of the file is:
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/pdeng/.local/bin
    */1 *   *   *   *   dbrun

    You may have noticed since I put dbrun in ~/.local/bin, I include this directory in the $PATH environment variable. (Actually other scripts like my VPN wrapper is also there, so I have to do this).

  5. Now to issue a command, run dbrun 'new command' in the controlling system, you will notice the Dropbox systray icon start animating to indicate the synchronization of the modified command.txt file. The “new command” will be executed soon in the controlled system.

The default location I use to store the command.txt file in the script is ~/Dropbox/command.txt. You can change it to whatever you like by opening the script and changing the value of cmdfile(around line 11).

Security Concern

It is possible to modify the script to let it only run filtered safe commands you selected yourself. But as long as you are not stupid enough to mark the command.txt as “shared” in your Dropbox and you don’t run the script as root, I don’t see (for now) too much danger. Maybe I am too naïve so please let me know in the comment if there is any potential security issue that I overlooked. After all, I only let it help getting my home PC reachable and SSH will be used to finish all other things afterward.

Update: I just did a quick check on Dropbox’s wiki (didn’t know about this wiki before) and there is already a similar solution for remote control. The advantages of that one is a) it has smaller granularity as it runs in a big loop and check the file every 10 seconds. b) it redirect the output into another file which can be very useful sometimes. Maybe next I should do some search in Google before writing on my own, hehe …

Advertisements

About this entry