Sign in

How does a local script execute a script from a remote host?

user619964 edited in Thu, 08 Sep 2022

In the online deployment of Vue project, it's too complicated to package locally, upload to the remote host, decompress and so on. So I wrote a shell script locally to handle it with one click. The functions that have been realized now are local packaging, compression, uploading to remote host, logging on to remote host, and switching to corresponding directory. But the next operations, such as decompressing and deleting the zip package .. It's not going to work.

At first, I wanted to execute all the commands in the local shell script, but after logging in to the remote host and successfully switching to the corresponding directory, the later operations would not be executed. The code is as follows:

npm run build
cd ./dist
zip -r 'prestom_frontend.zip' './'
scp prestom_frontend.zip [email protected]:/home/app/presto_monitor_frontend
scp prestom_frontend.zip [email protected]:/home/app
ssh [email protected] -t 'cd /home/app/presto_monitor_frontend; bash --login'
// 以下操作不执行
ssh [email protected] -t 'rm -rf index.html favicon.ico static; bash --login'
ssh [email protected] -t 'unzip prestom_frontend.zip; bash --login'
ssh [email protected] -t 'rm -rf prestom_frontend.zip; bash --login'

I think of the second way, which is the deletion and other operations. I create a shell script remotely, and then I execute it remotely, which is OK. But how to execute the remote script in the local script? If you can, you don't have to log in to remote operation, ask the great God for advice

6 Replies
commented on Thu, 08 Sep 2022

Your script is too complex. Just synchronize Rsync to a remote directory?

npm run build
# 注意--delete会删除远程目录下多余的文件,确保你的远程目录是对的,防止误删文件
rsync -aPz --delete dist/ [email protected]:/home/app/presto_monitor_frontend/

This is done, static resource replacement does not need to restart nginx, so as long as you ensure that your nginx is running, so the deployment is over, refresh the next page to see the new version.

commented on Fri, 09 Sep 2022

You can write an HTTP interface in the remote server to perform compilation and deployment operations. You only need to access the interface locally

commented on Fri, 09 Sep 2022

ssh root @ ip " cd / dir " This one " cd / dir " It is the command executed on the remote computer after SSH connection

commented on Fri, 09 Sep 2022

ssh xx @ Full path of the host name shell script

commented on Fri, 09 Sep 2022

There are some differences between remote SSH and local execution. Similar to your operation, it is very convenient to either use Ci of code management or use fabric

commented on Fri, 09 Sep 2022

Each SSH will establish a new connection. So the changes you make to the environment in the first SSH (change the current directory with CD) have no effect on the subsequent commands.

If you want to do a lot of things, you can use bash - C:

ssh [email protected] bash -c "'
  cd some_directory
  rm some_file
  unzip some_other_file.zip

However, in this way, the parameter segmentation and variable replacement of the string after - C will be more complex. It's wrapped in two quotation marks, and that's why. (the whole SSH command is interpreted by the local bash. After - C, the string is sent to the server and interpreted by bash again.) If there are variables in it, it's easy to make a mess.

So it was changed to this:

script=$(cat  <<SCRIPT
  cd some_directory
  rm some_file
  unzip some_other_file.zip
ssh [email protected]st bash -c "'$script'";

Here we use here documents. But it can only slightly alleviate the trouble of quotation mark escape.

(of course, in the end, we turned to ansible. It's too complicated to write our own scripts ...)