Tasker + Termux = Android + Mac

Thanks to a helpful post by William Denton, I remembered the Android utility Tasker, and realized that I could use it to automate the gluing together of my Android phone and my Mac.

Here’s what I did.

First, I installed Tasker on my phone from Google Play and then the Termux:Task add-on from F-Droid.

Next, on my phone I launched Termux, created a directory ~/.termux/tasker, and created two scripts there.

The first, start-ssh, to start the SSH server on the phone (after killing it first, in case it was running already):

#!/bin/bash

pkill sshd
sshd

The second, mount-phone-on-mac, to mount the phone on the Mac (by SSHing to the Mac and doing the mount from there, unmounting it first in case it was already mounted):

#!/bin/bash

ssh peter@192.168.2.2 "umount ~/motog7"
ssh peter@192.168.2.2 "/usr/local/bin/sshfs phone:/storage/emulated/0 ~/motog7 -o volname=motog7 -p 8022"

To allow this second script to work properly, I had to do a couple of things.

First, in System Preferences on the Mac, I had to check the “Remote Login” box on the Sharing sheet.

Second, on the phone, in Termux, I had to create an SSH keypair:

ssh-keygen -t rsa -b 2048 -f id_rsa

This left me with a public key file ~/.ssh/id_rsa.pub that I then added to the end of ~/.ssh/authorized_keys on my Mac.

I made these two scripts executable with:

chmod +x ~/.termux/tasker/*

I then tested them out to make sure they worked.

Next, I set up Tasker.

I created a new Profile called Reinventorium with the trigger of State > Wifi Connected and the SSID of my office’s wireless network:

Screen shot of Tasker Profile

I added a new Task called Mac to this Profile, and added two Actions to that Task, one for each of the two scripts I created above, selecting Plugin > Termux:Task from the Action category and entering the name of the script:

Screen shot of Tasker Task

With all of this in place, every time I come into the office with my phone and it connects to the office wireless network, it automatically launches its SSH server, and tells my Mac to mount its storage.