Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript send commands through Terminal

Need Applescript to open Terminal, then ssh testname@192.168.1.10, and then type in the password, then type in "sudo poweroff", then type in the password again.

Right now I have


tell application "Terminal"

do script ("ssh testname@192.168.1.10;")

delay 6

do script ("password")

do script ("sudo poweroff")

delay 6

do script ("password")

end tell


but Terminal stops and asks for a password response for the initial ssh request.


I'm trying to have my mac Applescript (from a calendar event daily) run this to shutdown another computer (linux) on the house network.

NOTE: if I manually open Terminal on my mac, and just type in ssh testname@192.168.1.10, it asks for the password, I type it in, and it logs in. Then I just type in sudo poweroff , then it asks for the password again, I type it in, and the other machine shuts down without a problem.

So, my issue is Applescript is not telling Terminal to just do the same thing automatically from the applescript for some reason. I've tried just about everything I can think of and am stumped. I KNOW it can be done, because I can manually do it by tying in Mac Terminal. Just can't get Applescript to do it?


Posted on Sep 13, 2020 1:18 PM

Reply
Question marked as Best reply

Posted on Sep 14, 2020 11:19 AM

Besides switching to using keys vs. passwords for the login, the most obvious problem with your script is the race condition in the middle.


Specifically, your logic is:


ssh to remote machien

<wait for password prompt>

type password

type sudo command

<wait for password prompt>

type sudo password


Do you see the problem? There is no delay between entering the password for your ssh connection and entering the sudo command. Typically verifying identity and establishing the ssh connection takes a little time - I'm guessing you're passing the sudo command before the remote shell is ready to process it.


A simple pause between the ssh password and the sudo command may be all that's required.


However, it is strongly advised that you a) switch to key-based authentication to remove the need to type your password to initiate the SSH connection, and b) you learn about /etc/sudoers on the remote machine to avoid the need to type your password again after the sudo command.

4 replies
Question marked as Best reply

Sep 14, 2020 11:19 AM in response to thomjw

Besides switching to using keys vs. passwords for the login, the most obvious problem with your script is the race condition in the middle.


Specifically, your logic is:


ssh to remote machien

<wait for password prompt>

type password

type sudo command

<wait for password prompt>

type sudo password


Do you see the problem? There is no delay between entering the password for your ssh connection and entering the sudo command. Typically verifying identity and establishing the ssh connection takes a little time - I'm guessing you're passing the sudo command before the remote shell is ready to process it.


A simple pause between the ssh password and the sudo command may be all that's required.


However, it is strongly advised that you a) switch to key-based authentication to remove the need to type your password to initiate the SSH connection, and b) you learn about /etc/sudoers on the remote machine to avoid the need to type your password again after the sudo command.

Sep 17, 2020 6:42 PM in response to thomjw

You do not really need the Terminal for that. You can do it all from an Applications -> Automator -> Run Shell Script, setup as a double clickable app


If you do decided to go for passwordless ssh-keygen keys, or storing your ssh-keygen key in your Keychain, then instead of needing to use sudo, you put your ssh-keygen .pub key into the /var/root/.ssh/authorized_keys file


Now you can

ssh root@remote.machine.address command args

You will be logging in as root on that system, so you do not need sudo, and you can just put the command and its arguments on your ssh command line.


Google can help you with things like creating ssh-keygen keys (with or without a password). If you want to store your local private key in your Keychain, again Google can help you there.


Anyway, once you have the ssh-keygen key in the remote system's root account authorized_keys file, you just stick the above ssh command into the Automator app. Save. And now when you double click on the app, it will execute that ssh command. No terminal necessary.

Applescript send commands through Terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.