views:

1425

answers:

7

Hi, I want to do something like this:

tell application "Terminal"
  activate
  do script "ssh [email protected]"
  -- // write user's password
  -- // write some linux commands to remote server
end tell

For example to log in to the server, enter the password, and then login to mysql and select a DB.
I type that every day and it would be really helpful to bundle it into a script.

Also, is there a reference of what commands, properties, functions, etc. do applications (Terminal, Finder, etc) have available to use within Applescript? thanks!

EDIT: Let me clear this up: I don't want to do several 'do script' as I tried and doesn't work. I want to open a Terminal window, and then emulate a human typing in some characters and hitting enter. Could be passwords, could be commands, whatever, just sending chars to the Terminal which happens to be running ssh. I tried keystroke and doesn't seem to work.

A: 

This is straight out of the help for Terminal:

To view Terminal’s AppleScript dictionary:

Drag the Terminal icon onto the Script Editor icon, located in the AppleScript folder in the Applications folder

Works for me. I opened the Applications/Utilities folder in Finder and dragged the Terminal icon from there. Works the same for other apps.

martin clayton
Thanks, it's a good one, but looks like Terminal only has 'do script' and nothing like send string or something. I tried keystroke that works with other apps, but not with Terminal.
Petruza
+1  A: 

I could be mistaken, but I think Applescript Terminal integration is a one-shot deal...That is, each do script call is like opening a different terminal window, so I don't think you can interact with it at all.

You could copy over the SSH public keys to prevent the password prompt, then execute all the commands joined together (warning: the following is totally untested):

tell application "Terminal"
    activate
    do script "ssh [email protected] '/home/jdoe/dosomestuff.sh && /home/jdoe/dosomemorestuff.sh'"
end tell

Alternatively, you could wrap the ssh and subsequent commands in a shell script using Expect, and then call said shell script from your Applescript.

EvanK
You can send a series of commands to one Terminal shell just by using multiple "do scripts". Append "in window 0" and they use the frontmost window I believe.
martin clayton
No, it seems that do script actually tries to execute a script (as you would expect) which in some way is different than just sending the string to the terminal and hitting enter, because it didn't work as if I just entered the password.
Petruza
what about `do shell script ssh user:[email protected]` to avoid the password prompt.
stib
+1  A: 

set up passwordless ssh (ssh-keygen, then add the key to ~/.ssh/authorized_keys on the server). Make an entry in ~/.ssh/config (on your desktop), so that when you run ssh mysqlserver, it goes to user@hostname... Or make a shell alias, like gotosql, that expands to ssh user@host -t 'mysql_client ...' to start the mysql client interactively on the server.

Then you probably do need someone else's answer to script the process after that, since I don't know how to set startup commands for mysql.

At least that keeps your ssh password out of the script!

Peter Cordes
Ok thanks. But the password was just an example, the point here is that I want to send several strings to the terminal, no matter if they're passwords, mysql commands or whatever.
Petruza
Yeah, I got that. But we sysadmins _hate_ it when people put their passwords into plain text files all over the place.
Peter Cordes
:) Ok, thanks for the advice. But I'm the only user in my computer, and besides, the user and password I use to log to the ssh server is shared by a lot of employees all over the world so security doesn't look to be an issue for the company.
Petruza
/facepalm. It doesn't matter that you're the only user. File permissions defend against other users on the same machine. It's viruses and other attack vectors into your account that are the concern. If pwning your machine gives direct access to the more-valuable server, that's a bad thing.
Peter Cordes
Do you even know about Mac OS and the subject here or just wanted to make a sysadmin rant? You didn't even try to answer the question. That's what this is about, making questions and answering them. Thanks.
Petruza
Yeah, sorry I got carried away with the sysadmin rant. My answer started as a suggestion for an alternative way to do what you want with just ssh features, but then I realized you needed to do a bunch of interactive stuff on the remote side, so it didn't end up being as helpful as I hoped, and then I got the rant rolling. I have used OS X, and I have scripted terminal sessions with mrxvt on Linux, but I haven't scripted OS X's terminal. Sorry for being a jerk. :(
Peter Cordes
+3  A: 

As EvanK stated each do script line will open a new window however you can run
two commands with the same do script by separating them with a semicolon. For example:

tell application "Terminal"
    do script "date;time"
end tell

But the limit appears to be two commands.

However, you can append "in window 1" to the do script command (for every do script after the first one) to get the same effect and continue to run as many commands as you need to in the same window:

tell application "Terminal"
    do script "date"
    do script "time" in window 1
    do script "who" in window 1
end tell


Note that I just used the who, date, and time command as an example...replace with whatever commands you need.

Mason Rove
A: 

Petruza,

Instead of using keystroke use key code.
The following example should work for you.

tell application "System Events"
    tell application process "Terminal"
     set frontmost to true
     key code {2, 0, 17, 14}
     keystroke return
    end tell
end tell

The above example will send the characters {d a t e} to Terminal and then
keystroke return will enter and run the command. Use the above example
with whatever key codes you need and you'll be able to do what you're trying to do.

Mason Rove
A: 

what about something like this:

tell application "Terminal"

activate
do shell script "sudo dscl localhost -create /Local/Default/Hosts/cc.josmoe.com IPAddress 127.0.0.1"
do shell script "sudo dscl localhost -create /Local/Default/Hosts/cc.josmos2.com IPAddress 127.0.0.1"

end tell

joey
+1  A: 

to send multiple commands to the same shell window:

tell application "Terminal"
  set currentTab to do script "echo do something"
  do script "echo do something else" in currentTab
  do script "echo do more" in currentTab
end tell
CRP