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.
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
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.
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:
cd /home/projects/
lftp and some arguments
mirror /projects/
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)