ixium Posted June 18, 2018 Share Posted June 18, 2018 Good morning, this is one I've been trying to accomplish a few times but have hit walls many times as my knowledge is limited. I have a computer in the office (imac) that we use as a display only. I want a simple workflow to run on my macbook that would sleep the machine. I can do it with a few commands manually by SSH into the machine, then run osascript -e 'tell application "System Events" to sleep'. But for some reason I cant get past this. I see 2 ways forward and havent figure out how to do it either way. Firstly I believe theres a way to send the command via SSH without first logging in, but I cant get this to do anything so im sure my syntax is wrong, I am trying to do it like this ssh username@192.168.1.xx 'osascript -e 'tell application "System Events" to sleep'' so im sure thats wrong as when I run that from my computer nothing at all happens. So in light of that not running I was thinking, make alfred run the ssh command, then paste in the second part osascript -e 'tell application "System Events" to sleep' press enter, wait then clsoe terminal, but for some reason this isnt working either, which makes no sense as Im doing the same thing I manually do just through alfred simulations Not really sure what to do, anyone got any ideas? I feel like its really simple and I just lack the knowledge Link to comment
deanishe Posted June 18, 2018 Share Posted June 18, 2018 (edited) 28 minutes ago, ixium said: Firstly I believe theres a way to send the command via SSH without first logging in A machine that didn't require you to log in would be incredibly insecure. What you're looking for is key-based authentication, which logs you in without needing your password. In fact, it's a good idea to disallow password-based authentication when you have your keys working. 28 minutes ago, ixium said: im sure my syntax is wrong Your quoting is wrong. And it's very difficult to pass a command with quotes in quotes because you need to escape the escapes. It's way simpler to save your command as a script and then run that via ssh: ssh the.host.name osascript Desktop/sleep.scpt Edited June 18, 2018 by deanishe Link to comment
CJK Posted June 18, 2018 Share Posted June 18, 2018 @ixium, since you're logging into a remote shell anyway, you could instead use a shell command to send the computer to sleep: ssh <host> pmset sleepnow @deanishe, how do you do inline code formatting ? Or could you direct me to a page for formatting posts on this forum ? Many thanks. Link to comment
deanishe Posted June 18, 2018 Share Posted June 18, 2018 (edited) 1 minute ago, CJK said: how do you do inline code formatting ? https://github.com/vitorgalvao/alfred-workflows/tree/master/MarkdownTransform I write in Markdown and convert it to BBCode because the editor on this forum is appalling. Edited June 18, 2018 by deanishe CJK 1 Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 Im sorry I should have clarified, I do have keys set up I believe as it doesnt require password anymore Link to comment
CJK Posted June 18, 2018 Share Posted June 18, 2018 Just now, deanishe said: because the editor on this forum is appalling. Agreeeeeeeeeeeeeeeeed, Thanks for the link. Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 gonna take a stab at all this now will let you know what I see Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 ssh <host> pmset sleepnow Seems to do exactly what I needed, thank you so much! Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 Worked perfect once, now its hanging, not sure whats going on, may try the other method Link to comment
vitor Posted June 18, 2018 Share Posted June 18, 2018 For completeness, the correct way to quote your original command is 'osascript -e "tell application \"System Events\" to sleep"'. Single quotes on the outside, double quotes inside those, escaped double quotes inside those. Link to comment
deanishe Posted June 18, 2018 Share Posted June 18, 2018 You don't actually need to quote the whole thing (ssh accepts more than one argument), just the AppleScript code after -e. Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 Hmm so the code is working great (was having a hard time with being on the right network) but now Im running into how to close terminal after the command is complete. I can send through the command+q command and that works well, except when the command is still running, any way to make it exit after finishing the command? I know im knit-picking now so no problem if not! Thank you both for your amazing help Link to comment
deanishe Posted June 18, 2018 Share Posted June 18, 2018 (edited) 37 minutes ago, ixium said: but now Im running into how to close terminal after the command is complete. It sounds like you're using Alfred's Terminal Command, which is not appropriate. That's only for when then command is interactive or you need to see the output. Use a Run Script with Language = /bin/bash instead. FWIW, when a Terminal Command is appropriate, and you want to close the session when the command exits, add && exit to your command. That will tell the shell to exit if the previous command is successful. It you want the shell to exit even if the command failed, add ; exit: ssh host.domain.com pmset sleepnow && exit Edited June 18, 2018 by deanishe Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 yep that what I was using, but how would I run this command through script ssh <host> pmset sleepnow Wouldnt I need to call terminal and everything, sorry im very novice with all of this Link to comment
deanishe Posted June 18, 2018 Share Posted June 18, 2018 (edited) 6 minutes ago, ixium said: Wouldnt I need to call terminal and everything No. I just told you how to run it. Add a Run Script action with Language set to "/bin/bash" (which is exactly what Terminal runs when you start a new session). Then paste your command in the box. A terminal is a GUI program (usually) for running a shell. The shell (e.g. bash or zsh or fish) is the program you're actually using. That's the software that you're passing the ssh command to. All Terminal does is pass your input to the shell and make its output look pretty. Edited June 18, 2018 by deanishe Link to comment
ixium Posted June 18, 2018 Author Share Posted June 18, 2018 Wow thank you for taking the time to explain that to me, I understand a little more and now this command is working flawlessly, thank you so much! You are always an amazing help deanishe Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now