【问题标题】:Shell script (on Mac OS) to SSH into a Linux/Unix server and run a commandShell 脚本(在 Mac OS 上)通过 SSH 连接到 Linux/Unix 服务器并运行命令
【发布时间】:2019-01-21 00:55:59
【问题描述】:

我想编写一个在 Mac OS 系统上运行的 shell 脚本,该脚本将执行以下操作:

  • 通过 SSH 连接到 Linux 服务器(使用密钥身份验证)
  • 通过 SSH 在服务器上执行命令

我认为它会是这样的:

ssh user@ip --[some kind of key auth flag here]
command --whatever-flags-here

不幸的是,我没有 Mac,也没有任何使用它们的经验,所以我希望你们能帮我解决这个问题。

【问题讨论】:

标签: macos terminal sh


【解决方案1】:

你可以这样做:

ssh user@ip [ssh-options] "command1; command2; command3; ..."

例如:

ssh user@ip "touch test.txt; cp test.txt test2.txt"

将在远程机器上创建两个空文件 test.txt 和 test2.txt。

另一种方法是使用标准输入重定向

ssh user@ip [ssh-options] < your_commands.sh

your_commands.sh 是在远程机器上执行的脚本。

【讨论】:

    猜你喜欢
    • 2010-11-03
    • 2021-09-04
    • 2012-12-23
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2021-01-17
    • 2015-09-06
    相关资源
    最近更新 更多