使用 Applescript 将命令和字符串发送到 Terminal.app
我想做这样的事情:
tell application "Terminal"
activate
do script "ssh [email protected]"
-- // write user's password
-- // write some linux commands to remote server
end tell
例如登录服务器,输入密码,然后登录mysql并选择一个DB。
我每天都会输入这些内容,将其捆绑到脚本中确实会很有帮助。
另外,是否有关于应用程序(终端、Finder 等)可在 Applescript 中使用的命令、属性、功能等的参考?谢谢!
编辑:让我澄清一下: 我不想做几个“执行脚本”,因为我尝试过但不起作用。 我想打开一个终端窗口,然后模拟人类输入一些字符并按 Enter 键。可能是密码,可能是命令,无论如何,只是将字符发送到恰好运行 ssh 的终端。我尝试了按键,但似乎不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
正如 EvanK 所说,每个 do 脚本行都会打开一个新窗口,但是您可以使用相同的 do 脚本运行两个命令,方法是用分号分隔它们。例如:
但限制似乎是两个命令。
但是,您可以将“在窗口 1 中”附加到 do 脚本命令(对于第一个之后的每个 do 脚本)以获得相同的结果生效并继续在同一窗口中运行所需数量的命令:
请注意,我只是使用 who、date 和 time 命令作为示例...替换
使用您需要的任何命令。
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:
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:
Note that I just used the who, date, and time command as an example...replace
with whatever commands you need.
这是另一种方法,但其优点是启动终端,将其置于最前面,并仅创建一个窗口。
当我想要整齐地呈现我的脚本结果时,我喜欢这样。
Here's another way, but with the advantage that it launches Terminal, brings it to the front, and creates only one window.
I like this when I want to be neatly presented with the results of my script.
这个怎么样?不需要关键代码(至少在 Lion 中,之前不确定),并且子例程简化了主脚本。
下面的脚本将以用户“me”的身份 ssh 到 localhost,延迟 1 秒后输入密码“myPassw0rd”,发出 ls,延迟 2 秒,然后退出。
How about this? There's no need for key codes (at least in Lion, not sure about earlier), and a subroutine simplifies the main script.
The below script will ssh to localhost as user "me", enter password "myPassw0rd" after a 1 second delay, issue ls, delay 2 seconds, and then exit.
您不需要“告诉”终端执行任何操作。 AppleScript 可以直接执行 shell 脚本。
或者什么...
You don't need to "tell" Terminal to do anything. AppleScript can do shell scripts directly.
or whatever ...
为什么不使用期望:
Why don't use expect:
你的问题具体是关于如何让 Applescript 做什么
你想要的。但是,对于所描述的特定示例,您可能想要
寻找“期望”作为解决方案。
Your question is specifically about how to get Applescript to do what
you want. But, for the particular example described, you might want
to look into 'expect' as a solution.
有点相关,你可能想看看 Shuttle (http://fitztrev.github.io/shuttle/),它是 OSX 的 SSH 快捷菜单。
Kinda related, you might want to look at Shuttle (http://fitztrev.github.io/shuttle/), it's a SSH shortcut menu for OSX.
最后一个示例在 10.6.8(Build 10K549)下出现由关键字“pause”引起的错误。
将其替换为“等待”一词即可使其工作:
The last example get errors under 10.6.8 (Build 10K549) caused by the keyword "pause".
Replacing it by the word "wait" makes it work:
我可能错了,但我认为 Applescript 终端集成是一次性的......也就是说,每个
do script
调用就像打开一个不同的终端窗口,所以我认为你不能与它互动。您可以复制 SSH 公钥以防止出现密码提示,然后执行所有连接在一起的命令(警告:以下内容完全未经测试):
或者,您可以将 ssh 和后续命令包装在使用 Expect 创建 shell 脚本,然后从 Applescript 中调用所述 shell 脚本。
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):
Alternatively, you could wrap the ssh and subsequent commands in a shell script using Expect, and then call said shell script from your Applescript.
设置无密码 ssh(
ssh-keygen
,然后将密钥添加到服务器上的~/.ssh/authorized_keys
)。在~/.ssh/config
(在桌面上)中创建一个条目,以便当您运行 ssh mysqlserver 时,它会转到 user@hostname...或者创建一个 shell 别名,例如 gotosql,扩展为ssh user@host -t 'mysql_client ...'
在服务器上以交互方式启动 mysql 客户端。那么您可能确实需要其他人的答案来编写该过程的脚本,因为我不知道如何设置 mysql 的启动命令。
至少这可以让你的 ssh 密码远离脚本!
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 tossh 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!
Petruza,
不要使用击键,而是使用键代码。
以下示例应该适合您。
上面的示例将发送字符 {date}
到终端,然后
按回车键将进入并运行
命令。将上面的示例
与您需要的任何键代码一起使用
你将能够做你想做的事。
Petruza,
Instead of using keystroke use key code.
The following example should work for you.
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.
像这样的事情怎么样:
what about something like this:
作为一个巧妙的解决方案,请尝试 -
或
对于启动的外壳,您可以转到“首选项”>“外壳>如果没有错误则将其设置为退出。
就是这样。
As neat solution, try-
or
For the shell launched, you can go to Preferences > Shell > set it to exit if no error.
That's it.
我构建了这个脚本。它位于 Yosemite 中,是使用 AppleScript 的 bash 脚本来选择 SSH 服务器的用户列表。基本上你定义一个IP,然后定义用户名.. 当应用程序启动时,它会询问你想以谁的身份登录.. SSH 终端启动并登录,提示输入密码...
不过,我强烈推荐; Nathan Pickmans 在上面发布关于 Shuttle 的内容 (http://fitztrev.github.io/shuttle/)..一个非常智能和简单的应用程序。
I built this script. It is in Yosemite and it is bash script using AppleScript to choose a list of users for SSH servers. Basically you define an IP and then the user names.. when the application launches it asks who you want to login in as.. the SSH terminal is launched and logged in prompting a password...
I highly recommend though; Nathan Pickmans post above about Shuttle (http://fitztrev.github.io/shuttle/).. a very smart and simple application.
首先连接到服务器并等待 6 秒(您可以更改它),然后使用同一选项卡在远程服务器上执行您需要的任何操作
First connect to the server and wait for 6 seconds (you can change that) and then execute whatever you need on the remote server using the same tab