繁体   English   中英

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

[英]Sending commands and strings to Terminal.app with Applescript

我想做这样的事情:

tell application "Terminal"
  activate
  do script "ssh user@server.com"
  -- // write user's password
  -- // write some linux commands to remote server
end tell

例如,要登录服务器,输入密码,然后登录mysql并选择一个数据库。
我每天都输入它,将它捆绑到脚本中会非常有用。

此外,是否有应用程序(终端,Finder等)可以在Applescript中使用的命令,属性,功能等参考? 谢谢!

编辑 :让我澄清一下:我不想做几个'做脚本',因为我尝试过但不起作用。 我想打开一个终端窗口,然后模拟人类输入一些字符并点击输入。 可能是密码,可能是命令,无论如何,只是将字符发送到正好运行ssh的终端。 我试过按键,似乎没有用。

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

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

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

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

但限制似乎是两个命令。

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

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


这是另一种方式,但它的优点是它启动终端,将它带到前面,并只创建一个窗口。

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

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

这个怎么样? 不需要密钥代码(至少在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

你不需要“告诉”终端做任何事情。 AppleScript可以直接执行shell脚本。

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

管他呢 ...

您的问题具体是关于如何让Applescript做你想做的事。 但是,对于所描述的特定示例,您可能希望将“期望”视为一种解决方案。

为什么不使用expect:

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

有点相关,你可能想看看Shuttle( http://fitztrev.github.io/shuttle/ ),它是OSX的SSH快捷菜单。

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

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

tell application "Terminal"
    activate
    do script "ssh jdoe@example.com '/home/jdoe/dosomestuff.sh && /home/jdoe/dosomemorestuff.sh'"
end tell

或者,您可以使用Expect将ssh和后续命令包装在shell脚本中,然后从Applescript调用所述shell脚本。

设置无密码ssh( ssh-keygen ,然后将密钥添加到服务器上的~/.ssh/authorized_keys )。 ~/.ssh/config (在你的桌面上)创建一个条目,这样当你运行ssh mysqlserver时,它会转到user @ hostname ...或者创建一个shell别名,比如gotosql,扩展为ssh user@host -t 'mysql_client ...'以在服务器上以交互方式启动mysql客户端。

然后你可能确实需要其他人的答案来编写脚本,因为我不知道如何为mysql设置启动命令。

至少这会使你的ssh密码不在脚本之外!

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}发送到终端然后
击键返回将进入并运行命令。 使用上面的例子
使用您需要的任何密钥代码,您将能够做您想做的事情。

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

用“等待”一词替换它可以使它工作:

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

作为整洁的解决方案,尝试 -

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

要么

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

对于启动的shell,您可以转到Preferences> Shell>如果没有错误则将其设置为退出。

而已。

我构建了这个脚本。 它位于Yosemite中,它是使用AppleScript为SSH服务器选择用户列表的bash脚本。 基本上你定义一个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/ )..一个非常智能和简单的应用程序。

这样的事情怎么样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM