This post describes how you can set up the automation app Tasker on android to run a command on a remote host via SSH, without the need to enter a password. We will discuss two options: The first one will just run a command, the second one can additionally return a value, but will require a paid plug-in.
Option 1: Run a remote command via ConnectBot
- Install the free SSH client ConnectBot.
- We need to set up key-based authentication with ConnectBot, and there are already tutorials on that, for example this one.
- When you have set up the connection, long-press the connection in ConnectBot’s host list and
Edit host. Change the name to some nickname, e.g., myconnection.
- Configure the post-login automation. Enter the command or script you’d like to run followed by a
semicolon, exit and enter, e.g.:
/home/me/somescript.sh; exit ↩. This is to make sure ConnectBot just runs the command and returns.
- Now let’s use it in Tasker. Create a Task, select
+, System, Send Intent. Configure the action as follows:
- Cat: None
- Target: Activity
When you run the Task, the remote command should execute. Note that for every different command you want to run on the host, you need to create a separate profile in ConnectBot, with different nicknames.
Option 2: Run a remote command and return its output to Tasker
- The SSH Plugin can not reuse ConnectBot’s key file format, so we need to create a key pair on the
target host. Log in to the host and run
ssh-keygen -t rsa. The command will ask where to save the file, select one that you will recognize for usage in Tasker, e.g.,
/home/me/.ssh/id_rsa-tasker. When asked for a passphrase, enter a good passphrase.
- Authorize the key:
cat ~/.ssh/id_rsa-tasker.pub >> ~/.ssh/authorized_keys
- Copy the file
~/.ssh/id_rsa-taskerto you android device somehow. One method is via the free ES File Explorer that also allows to copy files via SSH.
- In Tasker, create a Task, select
+, Plugin, SSH Plugin Paid.
- Configure the Action:
- Enter username and host
- For Keypair File Location, select the
id_rsa-taskerfile that you copied to your device
- For Encrypted Keyfile Password, enter the passphrase you selected during
- Enter the remote command followed by a semicolon
- Switch on “Return Output”
- In the last field, enter the name of a Tasker variable to bind to, e.g.,
- Now you can use the local
%resultvariable in subsequent actions of the task. The following steps are only required, if the executed command returns a line of text, and you need to get rid of the trailing newline.
- After the SSH Plugin action, add a Variable Set action, set its name to
%newlineand in its value field, enter a literal line break, i.e., press ↩.
- Add a Variable Split action, set its name to
%resultand splitter to
- Now you will have the result of the command without its trailing newline bound to the variable
Please go here to comment this article.