【问题标题】:Access SSH and git sync by shell script通过 shell 脚本访问 SSH 和 git sync
【发布时间】:2021-02-18 16:27:07
【问题描述】:

我们有一个服务器(托管),我们需要每天早上手动 git pull。 我们试图通过一个 shell 脚本文件来简化它。

# /bin/bash

# add key
ssh-add /Users/ownner/Documents/keys/key.pem
# Access the server
ssh myusername@domain.com -p18181
# Wait and type the password

# Each to check
echo "Hello, I am in the server now"

# Move to the folder
cd www/folder1/public_html/wp-content/plugins

# Git pull
find . -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;

在添加密码之前它工作正常。 但在那之后它就不起作用了。当我“退出” ssh 时,代码的工作量就会减少。

我相信我错过了一些东西。 仅供参考,我们实际上需要 git pull 6 个文件夹。 (文件夹 1、文件夹 2、文件夹 3、文件夹 4、文件夹 5、文件夹 6)。所以我们将使用“for each”作为循环。

您能否给我们一个提示,在登录(访问)服务器后 cd、git pull 是如何工作的?

谢谢,

【问题讨论】:

  • 检查man ssh。 ssh foo@bar [命令]。因此,您可以将命令附加到 ssh 行。

标签: shell ssh


【解决方案1】:

尝试将最后三个命令放在脚本中,在远程服务器中,在交互式 SSH 会话中。

您可以根据需要制作该脚本,查找文件夹并在每个文件夹上执行 git pull。
重点是:一旦脚本在远程服务器上运行,您就可以使用非交互式 SSH 调用来调用它:

ssh myusername@domain.com -p18181 /path/to/remote/script

注意:如果您有本地公共key.pub,您可以将其复制到远程服务器~myusername/.ssh/authorized_keys,您将不必再输入myusername 密码。 p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2013-06-02
    • 2014-03-29
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多