gpt4 book ai didi

macos - 从您的机器上通过 ssh 对 Google Compute 实例运行 Bash 脚本

转载 作者:行者123 更新时间:2023-12-02 14:12:12 24 4
gpt4 key购买 nike

我正在尝试在 Google Compute 实例上运行我机器上的脚本。我希望脚本能够连接并完成其工作,并且惊讶地看到下面的行为:

#!/bin/bash

gcloud compute ssh myinstance

echo "blah" > /tmp/test.txt
此脚本确实连接到 myinstance ,但它写 /tmp/test.txt在我的机器上,而不是计算实例上。脚本结束时 ssh 连接仍然存在,我的终端已连接。
我原以为它会连接,将文件写入计算实例,然后断开与 ssh 的连接。如何在我的机器上维护脚本,然后通过 ssh 轻松地针对各种计算实例运行?主要用例是从 Mac 到 Ubuntu。

最佳答案

WORK_PROJECTS_PATH="$HOME/path"

BACKEND_SCRIPT_NAME="redeploy_backend.sh"
LOCAL_BACKEND_SCRIPT_PATH="$WORK_PROJECTS_PATH/myproject/scripts/deploy/$BACKEND_SCRIPT_NAME"
REMOTE_BACKEND_SCRIPT_TEMP_PATH="/tmp/$BACKEND_SCRIPT_NAME"
REMOTE_BACKEND_SCRIPT_PATH="/home/path/$BACKEND_SCRIPT_NAME"

GCP_INSTANCE="myproject-staging"

gcloud config set project myproject_name
# move current version of script over each time to ensure it's up to date
gcloud compute ssh $GCP_INSTANCE --command="sudo rm -f $REMOTE_BACKEND_SCRIPT_PATH"
# sending to temp path allows you to skip entering the compute instance password
gcloud compute copy-files $LOCAL_BACKEND_SCRIPT_PATH $GCP_INSTANCE:$REMOTE_BACKEND_SCRIPT_TEMP_PATH

gcloud compute ssh $GCP_INSTANCE --command="sudo mv $REMOTE_BACKEND_SCRIPT_TEMP_PATH $REMOTE_BACKEND_SCRIPT_PATH"
gcloud compute ssh $GCP_INSTANCE --command="sudo chmod +x $REMOTE_BACKEND_SCRIPT_PATH"
# run the script on the server from your machine
gcloud compute ssh $GCP_INSTANCE --command="$REMOTE_BACKEND_SCRIPT_PATH"

关于macos - 从您的机器上通过 ssh 对 Google Compute 实例运行 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com