help to make simple shell script or Apple script

I’m trying to make a script to work with ChronoSync backup, that can shut down my NAS after backup has finished.

Shutting down the NAS with terminal commands is easy:

[i]ssh admin@[IP-of-NAS]

  • then password should be entered manually
    poweroff
    [/i]
  • and the NAS shuts down within a minute.

ChronoSync can run a shell script or Apple Script after the backup has ended. But how do I get a executable script out of the lines above?? In other words, I just want a script that connects to the NAS via SSH and tell it to poweroff.

Anyone who can help?

command should be something like this (didn’t checked it in applescript)

do shell script "echo \"spawn ssh user@server poweroff
expect \\\"Password:\\\" {send \\\"password\\r\\\"}\" | expect"

My last post of this thread is a more general way and is easy to change.

Thanks,

I typed this in Apple script editor :

do shell script “echo "spawn ssh [user]@[NAS IP] poweroff
expect \"Password:\" {send \"[my password]\r\"}" | expect”

compiled it and it returned this as the result:

"spawn ssh [user]@[NAS IP] poweroff
[user]@[NAS IP]'s password: "

what does this mean? The NAS did’nt power off. Can you point me one step further?

Use the code from another post of me (posted below). The first post of me I couldn’t test it and there is something wrong with the return. When I change it to string id 10 it works. But the script belows works much better because it also send an yes if there is question when you first connect to ssh (add host to known hosts file).

set sshUser to "sshuser"
set sshPasswd to "sshpasswd"
set sshServer to "server"
set externalCommand to "poweroff"


set thecommand to "(echo \"spawn ssh"
set thecommand to thecommand & " " & sshUser & "@" & sshServer
set thecommand to thecommand & " " & externalCommand & "\" ;"
set thecommand to thecommand & "echo \"expect {\" ;"
set thecommand to thecommand & "echo \"\\\"Password:\\\" {send \\\""
set thecommand to thecommand & sshPasswd
set thecommand to thecommand & "\\\\r\\\" ; exp_continue }\" ;"
set thecommand to thecommand & "echo \"\\\"yes/no?\\\" {send \\\"yes\\\\r\\\" ; exp_continue }\" ;"
set thecommand to thecommand & "echo \"eof\" ;"
set thecommand to thecommand & "echo \"}\" ;"
set thecommand to thecommand & ") | expect"

do shell script thecommand

okay,

this seems to give exactly the same result:

"spawn ssh [user]@[NAS IP] poweroff
[user]@[NAS IP]'s password: "


To me it looks like the script send the external command (poweroff) BEFORE sending the password, is that correct?

When I power off with Terminal, I first enter password, then types the command “poweroff”…

Nextg step ??

Thanks again for your help ! :slight_smile:

Sorry my fault.

You’re not connecting to a late Unix Mac server because Password is password on your remote machine. Change the capitial \"Password:\" to \"password:\" and everything works fine. The result is normal what you see by the way.

It’s not like you think it works. What this command does is running your command (first) and fetching stdout and send data back if it matches the condition to stdin of the process. It’s like GUI scripting in Mac OS X, if window “a window name” exists then…

EDIT: to clear out misunderstandings is that I Mean the first command that is run by expect is not poweroff but connecting ssh. The poweroff is a subcommand of ssh and has nothing directly to do with expect.

YES, you’re right. It works…:smiley: Thank you!

If I have to do multiple things in the ssh-session then?

The thing is, that we have a ftp setup for work, and when a new member upload any arts, it sends a mail to me, then it would be great if I could make an applescript that automatically downloaded that art.

The mail looks like:
Subject: New project-art (depends on project, could be other subjects too)
MailText: 2011-07-26 - 18:31 New.BorderFrame.For.Project.58294

Then I have to login to ssh to a remote machine, run commands:

  1. cd /home/projects/
  2. lftp and some arguments
  3. mirror /projects/
  4. when its finished, quit

Is this possible?

Model: iMac 27" i5
AppleScript: 2.2
Browser: chrome 13 / OS X Lion
Operating System: Mac OS X (10.6)

Late response, but it’s still unanswered: You’ll find the answer here