使用 Applescript 将命令和字符串发送到 Terminal.app

发布于 2024-08-14 04:53:49 字数 632 浏览 10 评论 0原文

我想做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

放低过去 2024-08-21 04:53:50

正如 EvanK 所说,每个 do 脚本行都会打开一个新窗口,但是您可以使用相同的 do 脚本运行两个命令,方法是用分号分隔它们。例如:

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

但限制似乎是两个命令。

但是,您可以将“在窗口 1 中”附加到 do 脚本命令(对于第一个之后的每个 do 脚本)以获得相同的结果生效并继续在同一窗口中运行所需数量的命令:

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

请注意,我只是使用 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:

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.

东北女汉子 2024-08-21 04:53:50

这是另一种方法,但其优点是启动终端,将其置于最前面,并仅创建一个窗口。

当我想要整齐地呈现我的脚本结果时,我喜欢这样。

tell application "Terminal"
    activate
    set shell to do script "echo 1" in window 1
    do script "echo 2" in shell
    do script "echo 3" in shell
end tell

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.

tell application "Terminal"
    activate
    set shell to do script "echo 1" in window 1
    do script "echo 2" in shell
    do script "echo 3" in shell
end tell
相对绾红妆 2024-08-21 04:53:50

这个怎么样?不需要关键代码(至少在 Lion 中,之前不确定),并且子例程简化了主脚本。

下面的脚本将以用户“me”的身份 ssh 到 localhost,延迟 1 秒后输入密码“myPassw0rd”,发出 ls,延迟 2 秒,然后退出。

tell application "Terminal"
    activate
    my execCmd("ssh me@localhost", 1)
    my execCmd("myPassw0rd", 0)
    my execCmd("ls", 2)
    my execCmd("exit", 0)
end tell
on execCmd(cmd, pause)
    tell application "System Events"
        tell application process "Terminal"
            set frontmost to true
            keystroke cmd
            keystroke return
        end tell
    end tell
    delay pause
end execCmd

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.

tell application "Terminal"
    activate
    my execCmd("ssh me@localhost", 1)
    my execCmd("myPassw0rd", 0)
    my execCmd("ls", 2)
    my execCmd("exit", 0)
end tell
on execCmd(cmd, pause)
    tell application "System Events"
        tell application process "Terminal"
            set frontmost to true
            keystroke cmd
            keystroke return
        end tell
    end tell
    delay pause
end execCmd
何以心动 2024-08-21 04:53:50

您不需要“告诉”终端执行任何操作。 AppleScript 可以直接执行 shell 脚本。

set theDir to "~/Desktop/"
do shell script "touch " & theDir &"SomeFile.txt"

或者什么...

You don't need to "tell" Terminal to do anything. AppleScript can do shell scripts directly.

set theDir to "~/Desktop/"
do shell script "touch " & theDir &"SomeFile.txt"

or whatever ...

呆橘 2024-08-21 04:53:50

为什么不使用期望:

tell application "Terminal"
    activate
    set currentTab to do script ("expect -c 'spawn ssh user@IP; expect \"*?assword:*\"; send \"MySecretPass
\"; interact'")
end tell

Why don't use expect:

tell application "Terminal"
    activate
    set currentTab to do script ("expect -c 'spawn ssh user@IP; expect \"*?assword:*\"; send \"MySecretPass
\"; interact'")
end tell
诺曦 2024-08-21 04:53:50

你的问题具体是关于如何让 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.

泅人 2024-08-21 04:53:50

有点相关,你可能想看看 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.

酒中人 2024-08-21 04:53:50

最后一个示例在 10.6.8(Build 10K549)下出现由关键字“pause”引起的错误。

将其替换为“等待”一词即可使其工作:

tell application "Terminal"
    activate
    my execCmd("ssh me@localhost", 1)
    my execCmd("myPassw0rd", 0)
    my execCmd("ls", 2)
    my execCmd("exit", 0)
end tell

on execCmd(cmd, wait)
    tell application "System Events"
       tell application process "Terminal"
          set frontmost to true
          keystroke cmd
          keystroke return
       end tell
    end tell

    delay wait
end execCmd

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:

tell application "Terminal"
    activate
    my execCmd("ssh me@localhost", 1)
    my execCmd("myPassw0rd", 0)
    my execCmd("ls", 2)
    my execCmd("exit", 0)
end tell

on execCmd(cmd, wait)
    tell application "System Events"
       tell application process "Terminal"
          set frontmost to true
          keystroke cmd
          keystroke return
       end tell
    end tell

    delay wait
end execCmd
梦晓ヶ微光ヅ倾城 2024-08-21 04:53:50

我可能错了,但我认为 Applescript 终端集成是一次性的......也就是说,每个 do script 调用就像打开一个不同的终端窗口,所以我认为你不能与它互动。

您可以复制 SSH 公钥以防止出现密码提示,然后执行所有连接在一起的命令(警告:以下内容完全未经测试):

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

或者,您可以将 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):

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.

浊酒尽余欢 2024-08-21 04:53:50

设置无密码 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 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!

≈。彩虹 2024-08-21 04:53:50

Petruza,

不要使用击键,而是使用键代码。
以下示例应该适合您。

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

上面的示例将发送字符 {date}
到终端,然后
按回车键将进入并运行
命令。将上面的示例
与您需要的任何键代码一起使用
你将能够做你想做的事。

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.

波浪屿的海角声 2024-08-21 04:53:50

像这样的事情怎么样:

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

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
云裳 2024-08-21 04:53:50

作为一个巧妙的解决方案,请尝试 -

$ open -a /Applications/Utilities/Terminal.app  *.py

$ open -b com.apple.terminal *.py

对于启动的外壳,您可以转到“首选项”>“外壳>如果没有错误则将其设置为退出。

就是这样。

As neat solution, try-

$ open -a /Applications/Utilities/Terminal.app  *.py

or

$ open -b com.apple.terminal *.py

For the shell launched, you can go to Preferences > Shell > set it to exit if no error.

That's it.

花开浅夏 2024-08-21 04:53:50

我构建了这个脚本。它位于 Yosemite 中,是使用 AppleScript 的 bash 脚本来选择 SSH 服务器的用户列表。基本上你定义一个IP,然后定义用户名.. 当应用程序启动时,它会询问你想以谁的身份登录.. SSH 终端启动并登录,提示输入密码...

(***
 * --- --- --- --- ---
 * JD Sports Fashion plc
 * Apple Script
 * Khaleel Mughal
 * --- --- --- --- ---
 * #SHELLSTAGINGSSHBASH
 * --- --- --- --- ---
***)

set stagingIP to "192.162.999.999"

set faciaName to (choose from list {"admin", "marketing", "photography_cdn"})

if faciaName is false then
    display dialog "No facia was selected." with icon stop buttons {"Exit"} default button {"Exit"}
else
    set faciaName to (item 1 of faciaName)

    tell application "Terminal"
        activate
        do script "ssh " & faciaName & "@" & stagingIP & ""
    end tell

end if

不过,我强烈推荐; 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...

(***
 * --- --- --- --- ---
 * JD Sports Fashion plc
 * Apple Script
 * Khaleel Mughal
 * --- --- --- --- ---
 * #SHELLSTAGINGSSHBASH
 * --- --- --- --- ---
***)

set stagingIP to "192.162.999.999"

set faciaName to (choose from list {"admin", "marketing", "photography_cdn"})

if faciaName is false then
    display dialog "No facia was selected." with icon stop buttons {"Exit"} default button {"Exit"}
else
    set faciaName to (item 1 of faciaName)

    tell application "Terminal"
        activate
        do script "ssh " & faciaName & "@" & stagingIP & ""
    end tell

end if

I highly recommend though; Nathan Pickmans post above about Shuttle (http://fitztrev.github.io/shuttle/).. a very smart and simple application.

玩世 2024-08-21 04:53:49

首先连接到服务器并等待 6 秒(您可以更改它),然后使用同一选项卡在远程服务器上执行您需要的任何操作

tell application "Terminal"
   set currentTab to do script ("ssh user@server;")
   delay 6
   do script ("do something remote") in currentTab
end tell

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

tell application "Terminal"
   set currentTab to do script ("ssh user@server;")
   delay 6
   do script ("do something remote") in currentTab
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文