You are not logged in.
I've seen lots of questions from people on how to automate an ssh connection but I don't remember ever seeing an applescript solution, especially when you don't have ssh keys setup on the 2 computers. I worked out the below method. The explanation of how it works is in the script.
You have to supply 4 things in the code; HOST, USER, PASS, and PROMPT. The PROMPT is the symbol used at the command prompt when connected to the remote computer. In general it will be either ">" or "#". The example code connects to a remote computer, cd's to the root of the drive, and gets a list (ls -al) of the directory.
-- This is how you can script ssh and send a username/password and perform some commands
-- We basically create a shell script in the applescript code and execute it using "sh -c" which allows us to pass a shell script as text to the command line
-- In the shell script we ssh into a computer, cd to the root of the drive, and perform a ls command, then exit
-- The major problem with scripting ssh is that ssh requires input from the command line. It doesn't want to be scripted.
-- To get around that the shell script uses the command line tool "expect", which is a tool to simulate a user entering commands at the command prompt
-- The key with expect is you have to know what to expect in response from the server
-- So basically you issue a command (eg. spawn ssh) and then an expect statement meaning wait until you receive a response on the command line and once that expect statement happens you send another command
-- For example, after issuing the ssh command we expect to receive a password prompt, then we send the password, then we expect the shell prompt (eg. #)... now we are logged in so we can start issuing commands to the other computer
-- To explain the back slashes... in a bash script we would have \" in the expect command to escape a quote. Since we're in applescript we have to escape each of those things because they have a special meaning to applescript so it becomes \\\" which is \\ to send a backslash and \" to send a quote
-- NOTE: with each send command we need to "enter" the command... thus the \\r to simulate pressing return
set shScript to "#!/bin/bash
expect -c \"
spawn ssh $USER@$HOST
send \\\"cd /\\r\\\"
send \\\"ls -al\\r\\\"
do shell script "sh -c " & quoted form of shScript
great but I have posted several times the same way with expect command . It's good you've posted it in code exchange
edit: it's post #5 here
Last edited by DJ Bazzie Wazzie (2011-08-17 05:05:02 pm)
Great DJ Bazzie Wazzie. I searched for ssh and expect but didn't find it. That's why I had to create it myself. I wish I did find yours!
In any case it's good to have both posted. We both use expect but a little differently. Plus I wrote mine like a shell script whereas you created a string. So both are valuable as both approaches have their merits. Thanks for pointing that out.