简体   繁体   English

Bash-将2个ssh调用合并为1个(带有可选和强制命令)

[英]Bash - combine 2 ssh calls into 1 (with optional and mandatory commands)

I have a script with 2 ssh commands. 我有一个包含2条ssh命令的脚本。 The SSH scripts uses SSH to log into a remote server and deletes docker images. SSH脚本使用SSH登录到远程服务器并删除docker映像。

ssh person@someserver.com 'set -x &&

   echo "Stop docker images" ;
   sudo docker stop $(sudo docker ps -a -q) ;
   sudo docker rmi -f $(sudo docker images -q) ;
   sudo docker rm -f $(sudo docker ps -a -q)'

Note use of ; 注意用法; to separate commands (we don't care if one or more of the commands fail). 分隔命令(我们不在乎一个或多个命令是否失败)。

The 2nd ssh command uses SSH to log into the same server, grab a docker compose file and run docker. 第二个ssh命令使用SSH登录到同一服务器,获取docker compose文件并运行docker。

ssh person@someserver.com 'set -x &&

   export AWS_CONFIG_FILE=/somelocation/myaws.conf &&
   aws s3 cp s3://com.somebucket.somewhere/docker-compose/docker-compose.yml . --region us-east-1 &&

   echo "Get ECR login credentials and do a docker compose up" &&
   sudo $(aws ecr get-login --region us-east-1) &&
   sudo /usr/local/bin/docker-compose up -d'

Note use of && to separate commands (this time we do care if one or more of the commands fail as we grab the exit code ie exitCode=$? ). 请注意,使用&&分隔命令(这一次我们会注意一个或多个命令是否会失败,因为我们抓住了退出代码,即exitCode=$? )。

I don't like the fact I have to split this into 2 so my question is can these 2 sections of bash commands be combined into a single SSH call (with both ; and && combinations)? 我不喜欢必须将其拆分为2个事实,所以我的问题是bash命令的这2个部分可以合并为一个SSH调用(同时包含;&&组合)吗?

Although it is possible to pass a set of commands as a simple single-quoted string, I wouldn't recommend that, because: 尽管可以将一组命令作为简单的单引号字符串传递,但我不建议这样做,因为:

  • internal quotation marks should be escaped 内部引号应转义
  • it is difficult to read (and maintain!) a code that looks like a string in a text editor 在文本编辑器中很难读取(和维护!)看起来像字符串的代码

I find it better to keep the scripts in separate files, then pass them to ssh as standard input: 我发现最好将脚本保存在单独的文件中,然后将它们作为标准输入传递给ssh

cat script.sh | ssh -T user@host -- bash -s -

Execution of several scripts is done in the same way. 几个脚本的执行以相同的方式完成。 Just concatenate more scripts: 只需串联更多脚本:

cat a.sh b.sh | ssh -T user@host -- bash -s -

If you still want to use a string, use a here document instead: 如果仍然要使用字符串,请改用here文档

ssh -T user@host -- <<'END_OF_COMMANDS'
# put your script here
END_OF_COMMANDS

Note the -T option. 注意-T选项。 You don't need pseudo-terminal allocation for non-interactive scripts. 对于非交互式脚本,不需要伪终端分配。

ssh person@someserver.com 'set -x;

   echo "Stop docker images" ;
   sudo docker stop $(sudo docker ps -a -q) ;
   sudo docker rmi -f $(sudo docker images -q) ;
   sudo docker rm -f $(sudo docker ps -a -q) ;

   export AWS_CONFIG_FILE=/somelocation/myaws.conf &&
   aws s3 cp s3://com.somebucket.somewhere/docker-compose/docker-compose.yml . --region us-east-1 &&

   echo "Get ECR login credentials and do a docker compose up" &&
   sudo $(aws ecr get-login --region us-east-1) &&
   sudo /usr/local/bin/docker-compose up -d'

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

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