Sending SSH Command to Remote Mac (iTunes / play-pause)

ssh
execbinding
itunes
Tags: #<Tag:0x00007f1e5c7d7658> #<Tag:0x00007f1e5c7d7478> #<Tag:0x00007f1e5c7d7270>
(Philipp) #37

thats not my personality :wink: - i edited my previous post

i dont get it :frowning: - thats my command line, that works
44

thats my .item:

Switch iTunes {nh="192.168.1.xxx", exec=">[*:/etc/openhab2/scripts/iTunes.sh]"}

LOG-Output

[INFO ] [ab.binding.exec.internal.ExecBinding] - executed commandLine 'ssh plauf@192.168.1.2 'osascript -e "tell application iTunes to playpause"''
0 Likes

(Scott Rushworth) #38

Try saving the items file again and watch the log. If there’s an error, it won’t load it and it will log the error. If it saves without error, then all I can think of is to restart OH.

0 Likes

(Philipp) #39

no error - no luck after restart :frowning:

0 Likes

(Scott Rushworth) #40

Does the script execute if you just run /etc/openhab2/scripts/iTunes.sh?

0 Likes

(Philipp) #41

it seems that i need a password again - wtf ?

55

EDIT:

Okay i fixed the password problem

0 Likes

(Scott Rushworth) #42

00

Based on this, openhab is the user that is running OH and will be running the script. I do not know openhabian, but I would try running the script as that user and setup the RSA key for that account. Once that is working, the Exec binding should be able to execute the script. Or you should be able to go back to the original way without the script.

1 Like

(Philipp) #43

i tried another way, i created a new iTunes2.sh file and it looks like this now:

02

When i try this script i get a “Permission denied”
( i already made i executable “sudo chmod u+x iTunes2.sh” )

[ERROR] [ab.binding.exec.internal.ExecBinding] - couldn't execute commandLine '/etc/openhab2/scripts/iTunes2.sh'
java.io.IOException: Cannot run program "/etc/openhab2/scripts/iTunes2.sh": error=13, Permission denied

0 Likes

(Scott Rushworth) #44

Check out the posts I linked. Your OH is running under the openhab account. That is the account that needs the permissions to run the script, or whatever else you want the Exec binding to execute.

1 Like

(Philipp) #45

you are probably right, and i understand that i need to change the user but i dont know how to do so :frowning: i am a beginner in the command line and do not understand why the openhabian image is that confusing for beginners – i thought its the hasslefree version :smiley: haha

thanks for all your ideas, links and help so far! learned a lot out of it so far :wink: thanks!

0 Likes

(Udo Hartmann) #46
sudo chown openhab: /etc/openhab2/scripts/iTunes2.sh

should fix this.

sudo                               : is for command as root
chown                              : change owner
openhab                            : user name
:                                  : change group name accordingly 
/etc/openhab2/scripts/iTunes2.sh  : name of file to change the ownership.
1 Like

(Philipp) #47

a pity - its not :frowning: Radio is still playing Music…

i think you didnt read the whole thread :wink: - i created two scripts, one with “openhabian” and one with “openhab” just for testing - but still no luck…

39

this is still working but my exec-binding is not :frowning:

0 Likes

(Udo Hartmann) #48

What’s the exact content of iTunes.sh and iTunes2.sh?

In question of file permissions: the simple way would be to

sudo chmod 755 /etc/openhab2/scripts/iTunes.sh

to allow editing only for user openhab, but reading and execution for all users. Later, when all things work the right way, you can change the permissions to a more safe configuration.

0 Likes

(Philipp) #49

both looks the same:

#!/bin/bash
toggleResult=`ssh username@192.168.1.xxx 'osascript -e "tell application \"iTunes\" to playpause"'`
echo "$toggleResult"

i tried your file permissions, but still no luck

0 Likes

(Udo Hartmann) #50

What’s the output of

sudo -u openhab /etc/openhab2/scripts/iTunes.sh

?

0 Likes

(Philipp) #51

i needed a password

i tried this - but whats the default password for the “openhab” user ?

45

0 Likes

(Udo Hartmann) #52

Aha!

So now the problem is reduced to the login via ssh :slight_smile:

User username is secured via password. There is no (good) way to use password for scripted ssh. Instead you should use private keys to login. The steps for this:

  1. Create a pair of keys either at your raspberry or at your mac with empty password (i.e. no password at all)
    At your raspberry this is done with ssh-keygen
  2. either copy the public key to your mac or copy the private key to your raspberry, depending on where you did the key creation
  3. move the private key to /etc/openhab2/.ssh/keyfile (where keyfile is a meaningful name, e.g. name of your mac)
  4. chmod this key file to 600 (sudo chmod 600 /etc/openhab2/.ssh/keyfile)
  5. at your mac, change to the user profile and add the public key to your system. As I don’t own a mac, I don’t know the exact steps to place the public key, so please use google for that.
  6. add the filename to your script:
toggleResult=`ssh -i /etc/openhab2/.ssh/keyfile username@192.168.1.xxx 'osascript -e "tell application \"iTunes\" to playpause"'`

This should result in a passwordless login for user openhab at your Mac with user username

1 Like

(Philipp) #53

i already created the keys, but as @5iver said,
i need to do this on the “openhab” user and not on the “openhabian” user and i dont know how to do so…

i tried “su openhab” but i dont know the default password !?
i found an interesting thread:

0 Likes

(Udo Hartmann) #54

No, you don’t need to.

The only thing to do is, to give the correct permissions and the correct file location.

There is no relation between private key and user account, in fact, you could use whatever private/public key pair you want to use:

  1. at the destination computer you need a public key, this key has to be allowed to be used to login by a user
  2. at the source computer you need an account with a private key
  3. you need to know the username corresponding to the public file

Altough there might be a username within the key file, it isn’t part of authentication process.

1 Like

(Philipp) #55

okay, but what to do ? :slight_smile: sorry for being confused with the exec-binding…

0 Likes

(Udo Hartmann) #56

:slight_smile:

This is no problem with exec binding, only a problem with correct permissions

0 Likes