Running ssh client to remote machine task

1,205 views
Skip to first unread message

Michael

unread,
Feb 18, 2012, 5:39:06 PM2/18/12
to Tasker
Hi,
I am trying to run some script on a remote machine using tasker.
I have btep pro installed with ssh binaries.
I wrote a test script that simply creates a file on the remote
machine.
Of course the appropriate public and private keys were created and the
private key was added to authorized on the target machine.
The private key was copied to /mnt/sdcard as well to make the path
shorter.
When run from btep's terminal window it works great (sh /mnt/sdcard/
testssh).
The file gets created and all is swell.
However if I try to run it from tasker using the play button for
testing nothing happens.
It appears something does go on as I see some traffic being sent/
received over the internet but the file is never created.
Also if I create a task that creates the file locally it works (file
gets created on the sdcard).
The phone is HTC Legend with 2.2 non rooted.

Pent

unread,
Feb 19, 2012, 4:49:26 AM2/19/12
to Tasker
You're using Run Shell with command /mnt/sdcard/testssh (not: sh /mnt/
sdcard/testssh) and Use Root clicked ? Timeout ?

Pent

Michael

unread,
Feb 19, 2012, 4:50:44 AM2/19/12
to Tasker
Ok I am terribly sorry I was able to fix it myself :)
Turns out I was being prompted to authorise the remote host key.
Adding -y to the command line fixed the issue.
Sorry again for false alarm , everything works great.

Michael

unread,
Feb 19, 2012, 4:51:03 AM2/19/12
to Tasker
Ok I am terribly sorry I was able to fix it myself :)
Turns out I was being prompted to authorise the remote host key.
Adding -y to the command line fixed the issue.
Sorry again for false alarm , everything works great.


On Feb 19, 11:49 am, Pent <supp...@apps.dinglisch.net> wrote:

Michael

unread,
Feb 19, 2012, 9:23:13 AM2/19/12
to Tasker
Ok turns out I still have some problem.
The same issue popped up with scp and I can't use the -y option as it
doesn't exist there.
Is there anyway to automate "y" press inside shell execute?

fubaya

unread,
Feb 19, 2012, 12:10:04 PM2/19/12
to Tasker
Open a terminal and run the command and it should save the host info
then it won't need to ask for our when ran with tasker

Michael

unread,
Feb 19, 2012, 12:14:35 PM2/19/12
to Tasker
fubaya, I did but it doesn't help.
After I ran the command with the stderr redirection I found out that
it adds an extra "/" right before the home part of the known_hosts
file path.
I also tried using output redirection to obtain remote file contents
from the remote machine.
Something along the lines : ssh blablabla "cat /filename" > /
localfilepath
It works when I run it from btep , but returns empty file if run from
tasker.

Michael

unread,
Feb 19, 2012, 12:58:37 PM2/19/12
to Tasker
Just an observation:
After some more testing it appears that it is impossible to write to
files using output redirection from tasker's shell task.
The resulting file appears as either 0 or 1 bytes long.
The same script works perfectly when run from BTEP's terminal.
Maybe Tasker uses a wrong shell?

fubaya

unread,
Feb 19, 2012, 1:32:49 PM2/19/12
to Tasker


On Feb 19, 12:14 pm, Michael <matrix...@gmail.com> wrote:
> fubaya, I did but it doesn't help.
> After I ran the command with the stderr redirection I found out that
> it adds an extra "/" right before the home part of the known_hosts
> file path.
> I also tried using output redirection to obtain remote file contents
> from the remote machine.
> Something along the lines : ssh blablabla "cat /filename" > /
> localfilepath
> It works when I run it from btep , but returns empty file if run from
> tasker.
>

try this: http://a-more-common-hades.blogspot.com/2010/11/ssh-error-and-fix.html

Michael

unread,
Feb 19, 2012, 1:42:19 PM2/19/12
to Tasker
Thanks fubaya,
Actually I am using the binary from btep pro.
I explicitly call it from its path by specifying it in the shell
script file which I call using sh /scriptpath/scriptname
The problem is not with ssh per se as it supports -y which supresses
all the prompts and lets me automate the ssh part just fine.
The problem lies with the scp that it uses (since I don't have root I
don't have control to which ssh binary it calls but I assume it
calls the right ssh anyway since it is all part of btep's package).
The problem is that the scp binary doesn't support such a command line
switch to suppress the unknown fingerprint warning.
It also doesn't support the StrictHostKeyChecking because it uses a
non standard ssh binary that doesn't support it as well.
I wouldn't mind if there was some other way to obtain remote file
contents on the device using output redirection, but this doesn't work
as well (see my previous post).
I am really lost as to what are my options.

TW

unread,
Feb 19, 2012, 2:18:32 PM2/19/12
to tas...@googlegroups.com
I am not using scp but am using ssh and rsync..
I am using also the BTEP binary for ssh and call everything from a SL4A script through Tasker
I am using public key authentication and don't need to use the -y, I just get the question once in the beginning and when I accept all the following connections work fine..

my phone is rooted and the binaries I'm calling from the script are in the BTEP dir...

Michael

unread,
Feb 19, 2012, 2:41:35 PM2/19/12
to Tasker
TW, I see your point.
Are you using the task or script task?
Also what interpreter do you use for that?

I assume my problem is that the environment is different when using
Tasker's shell task as opposed to BTEP's
Thats why ssh binary cannot find its known_hosts file where accepted
keys reside.
It works in BTEP's , but not in Tasker.

I understand I could probably use Sl4A and just call the script from
Tasker, but just for being able to download a file over ssh
it seems like an overkill.
Is there any chance to make either scp or output redirection work in
the shell task?

fubaya

unread,
Feb 19, 2012, 3:13:09 PM2/19/12
to Tasker
Try running this with Tasker "which scp > /sdcard/test" and see if
redirection works with something simple. I'm rooted but it works for
me running it without the root box checked. Mine says it's using the
scp in /system/xbin, which yours must be using as well. Have you tried
specifying the btep scp binary? I use scp all the time, so it
definitely works, but again, I'm rooted so that may be the difference.

TW

unread,
Feb 19, 2012, 3:15:51 PM2/19/12
to tas...@googlegroups.com
they are regular shell (bash) scripts
I call them from Tasker in a Task through the SL4A Plugin

SL4A may seem like overkill but on the other hand it might be usefull to be able to execute some line outside Tasker.
Below is a simple line I use to connect to one of my servers, as I use the BTEP ssh binary and a specific keyfile I prefer to have it in a script.
I can call this from Tasker to execute stuff automatically or do it from SL4A and quickly have a terminal open for some commands..

#!/bin/bash
/data/data/com.magicandroidapps.bettertermpro/bin/ssh -i /data/data/com.magicandroidapps.bettertermpro/home/.ssh/identity -l user 192.168.1.180

You have to use the full path to call the binary of BTEP if I remember correct (more than a year ago since I set this up :) )
I do think root is needed to call the binary this way or the identity file, not sure..
my phones don't stay unrooted long enough to test :)

the known_hosts file is also created under the ../home/.ssh dir..

another option might be to use ssh with the -y which seems to work for you and combine it with the cat command to read the file, the store that in a variable..
will look if I have an example somewhere..

fubaya

unread,
Feb 19, 2012, 3:23:35 PM2/19/12
to Tasker
On Feb 19, 3:15 pm, TW <thew...@gmail.com> wrote:
> another option might be to use ssh with the -y which seems to work for you
> and combine it with the cat command to read the file, the store that in a
> variable..
> will look if I have an example somewhere..

Once ssh is set up with keys, a typical login command works like:

ssh -i /path/to/key user@IP

To do a non-interactive connection and run a remote command, just add
the command in quotes after that. Everything in quotes is ran on the
remote machine, so, after the quotes, redirecting the output sends it
to a file on the phone.

ssh -i /path/to/key user@IP "cat /home/you/foo" > /sdcard/bar

TW

unread,
Feb 19, 2012, 3:24:53 PM2/19/12
to tas...@googlegroups.com
this is a combination of something I had and something in my head so I have no idea if it works :)

FILE_CONTENT=`/data/data/com.magicandroidapps.bettertermpro/bin/ssh -i /data/data/com.magicandroidapps.bettertermpro/home/.ssh/identity -l user 192.168.1.180 "cat /tmp/file/"

this should execute the cat command over ssh and store the result in FILE_CONTENT
now to find a way to get that in Tasker..
there is also the Secure Settings Plugin which is free and allow for the out and error variable to be used in Tasker..
I also use this one for some stuff..

anyway, if none of this works there might at least be something useful in it :)

TW

unread,
Feb 19, 2012, 3:28:30 PM2/19/12
to tas...@googlegroups.com
seems we posted at the same time :)

nice info..
I had quite some issues in the beginning with getting ssh to work after each rom flash (which was every few days during that time :) ) so I ended up with BTEP and the method I described above.
This was easy to restore with Titanium Backup after each flash and I was sure everything was up again in a short time

Michael

unread,
Feb 19, 2012, 3:39:19 PM2/19/12
to Tasker
Ok guys, thanks alot for all your help so far:)
I am going to try your suggestions one by one.
The locale secure settings plugin sounds like the best solution for
now.
I installed it now searching for it in Tasker :)
Will try other stuff as well and report back.
Just FYI I did try the backquote solution before and this didn't help :
(
Again like with others it works great in btep , but comes up short
when run from Tasker's shell task

Michael

unread,
Feb 19, 2012, 4:26:08 PM2/19/12
to Tasker
Ok guys you are really great :)
The security settings plugin works great and I finally can proceed
with my smart home widget :)
Thanks alot for all the help

baudi

unread,
Feb 19, 2012, 8:33:07 PM2/19/12
to tas...@googlegroups.com
smart home widget? care to share when you're done? :)

Michael

unread,
Feb 20, 2012, 6:21:14 AM2/20/12
to Tasker
Sure, once its ready :)
It won't be useful since my system is something I build specifically
for my home based on sis-pm surge protectors

fubaya

unread,
Feb 20, 2012, 8:35:47 PM2/20/12
to Tasker
If you have home automation that can be controlled from a command line
and you can ssh into the computer running it, or if it can be
controlled with http commands, it's just a matter of making something
with tasker or zoom to fit your fancy. I like simple text which when
tapped, triggers a ssh command to toggle lights or sensors on/off and
change the text color to indicate their state. I've had voice control
and tts feedback but never found it useful. But with buttons, sliders,
pop-ups, widgets, scenes, Zoom etc, the sky's the limit.

Clemens Fischer

unread,
Feb 21, 2012, 10:47:12 AM2/21/12
to tas...@googlegroups.com
Michael <matr...@gmail.com> wrote:

> I assume my problem is that the environment is different when using
> Tasker's shell task as opposed to BTEP's
> Thats why ssh binary cannot find its known_hosts file where accepted
> keys reside.
> It works in BTEP's , but not in Tasker.

I betcha! Go into any console, type "env" or "echo $HOME". Chances are ssh and many other unix type commands that have business with a users private files use the environment variable $HOME to find them.

Set "export HOME='directory-of-my-stuff'" before using ssh. Should work with busybox' "ash" and all Bourne shells.

+clemens

Clemens Fischer

unread,
Feb 21, 2012, 10:59:08 AM2/21/12
to tas...@googlegroups.com
TW <the...@gmail.com> wrote:

> this is a combination of something I had and something in my head so I
> have
> no idea if it works :)
>
> FILE_CONTENT=`/data/data/com.magicandroidapps.bettertermpro/bin/ssh -i
>
> > /data/data/com.magicandroidapps.bettertermpro/home/.ssh/identity -l
> user
> > 192.168.1.180 "cat /tmp/file/"`
>
>
> this should execute the cat command over ssh and store the result in
> FILE_CONTENT
> now to find a way to get that in Tasker..

... which might cause trouble.

- it only works for short files, because there's a limit on how much can be stored in a shell variable.

- using the variable will cause the shell to break its value on white space, expand '$any-letters' combinations etc. depending on weather it's quoted and how.

If the object is to be used as a file, store it in a file.

+clemens

Rick Macdonald

unread,
Feb 10, 2013, 6:39:38 PM2/10/13
to tas...@googlegroups.com


Michael,

I have a script file in /mnt/sdcard etc just as you described. The script runs "ssh" fine from the BTEP command line, but when run from Tasker I get an error (in the redirected output) that it couldn't open
/data/data/com.magicandroidapps.bettertermpro
/home//.ssh/known_hosts  (note the double "/")

You mentioned the problem of the extra "/" but didn't say how you fixed it, or if that was the cause of the open failure.

My phone is not rooted. It's a Galaxy Note II running Jelly Bean 4.1.2.

Regards,
Rick

Rick Macdonald

unread,
Feb 10, 2013, 7:40:58 PM2/10/13
to tas...@googlegroups.com


Some more info:

When I get the error about being unable to open the known_hosts file, it also says "Host 'xxx' key accepted unconditionally. This is because I'm using the -y flag to ssh.

If I run my BTEP script from Terminal Emulator, I get the same error about opening the known_hosts file, and the host key accepted unconditionally message. However, it does run the ssh remote command and returns the result along with the stderr messages.

I haven't been able to get Tasker to capture any of the output. Also, when I redirect the std_err and std_out messages when the script is run from Tasker, the output does not include the std-out from the command, only the std_err messages. (I redirect both together, and it does work as I said when run from the Terminal Emulator command line).

Also as I said, it runs cleanly from the BTEP command line (no problem with the keys).

Michael

unread,
Feb 12, 2013, 2:34:39 PM2/12/13
to tas...@googlegroups.com
Hi Rick,

I am not sure I remember what I meant by then, but probably I was referring to the front slash that was missing.
So I call my script like that using the secure settings plugin "sh /mypath/scriptname"
Hope this answers your question.
Just to clarify, did you install the secure settings plugin?

Michael

unread,
Feb 12, 2013, 2:40:20 PM2/12/13
to tas...@googlegroups.com
Regarding the keys I put them in a separate file on my sdcard's root  called identity so the actual shell script looks like that :
data/com.magicandroidapps.bettertermpro/bin/ssh -y hostname -p port -i /mnt/sdcard/identity -l username "script to run on remote system"

joaomgcd

unread,
Feb 12, 2013, 6:33:31 PM2/12/13
to tas...@googlegroups.com
Just wanted to chime in and let you know that you can easily do ssh commands in Tasker via AutoRemotehttp://goo.gl/VMkgw :)

Rick Macdonald

unread,
Feb 13, 2013, 2:16:04 PM2/13/13
to tas...@googlegroups.com


My command looks very much like yours. I had discovered that the identity file had to be in /mnt/sdcard, not because it's shorter to type but because (I believe) that location cannot be seen from outside BTEP. For example, when running the shell script from Terminal Emulator. I figure therefore that it needs to be there for Tasker as well.

The failure to open the known_hosts file is, I think, not because of the double "//" in the name that ssh generates, but because again, ssh run outside of BTEP cannot see that location (without root?). However, with the -y flag, the remote sshd "accepts the key unconditionally".

The remote script writes to a remote file so I can verify when the script does run and when it doesn't.

I can run the script from the command line in BTEP (cleanly) and Terminal Emulator (but with the failure to open the known_hosts file).

I can run the script from Tasker using Taskers Run Shell, using "sh /mnt/sshtest1", but it does not set the variable I specify to the output.

So far, I cannot get Secure Settings' Run Command to execute the remote script at all. I tried both with and without "sh" to call the script file. I think something broke Secure Settings. When I started this effort, I ran "date" locally to make sure it worked, which it did, but now it doesn't.

TomL

unread,
Feb 13, 2013, 4:10:14 PM2/13/13
to tas...@googlegroups.com
Maybe try running the shell command:

echo $PATH

from both secure settings and tasker, to see if there are any significant shell env differences?

Tom

Aled Thomas

unread,
Apr 3, 2013, 4:50:07 PM4/3/13
to tas...@googlegroups.com
you can use the tasker ssh command plugin to launch the script on a remote machine easily

Blake Hepner

unread,
Jul 6, 2013, 12:20:13 PM7/6/13
to tas...@googlegroups.com
Sorry michael for the unintended spam, I had never posted to a groups comment before, wrong button was chosen... twice.

To anyone having this issue I found a solution. You have to run the BTEP command as root from SL4A. e.g. you would run the command as su -c /data/data/com.magicandroidapps.bettertermpro/bin/ssh hostname -p port -i /mnt/sdcard/identity -l username "script to run on remote system"

That should allow it to check the known_hosts file properly.

IMPORTANT NOTE: You must have a ROOTED phone for this to work.
Reply all
Reply to author
Forward
0 new messages