什么是干净的方式通过SSH和运行多个命令在Bash的? [英] What's the Cleanest Way to SSH and Run Multiple Commands in Bash?

查看:140
本文介绍了什么是干净的方式通过SSH和运行多个命令在Bash的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个SSH代理设置,我可以在bash脚本在外部服务器上运行的命令做的东西,如:

I already have an ssh agent set up, and I can run commands on an external server in bash script doing stuff like:

ssh blah_server "ls; pwd;"

现在,我真的很想做的是一个外部服务器上运行长了很多的命令。封闭所有这些在引号之间会相当难看,而且我真的宁愿避免ssh方式连接多次,只是为了避免这一点。

Now, what I'd really like to do is run a lot of long commands on an external server. Enclosing all of these in between quotation marks would be quite ugly, and I'd really rather avoid sshing multiple times just to avoid this.

那么,有没有一种方法,我可以做到这一点一气呵成用括号括起来的东西?我在寻找的线沿线的东西:

So, is there a way I can do this in one go enclosed in parentheses or something? I'm looking for something along the lines of:

ssh blah_server (
   ls some_folder;
   ./someaction.sh;
   pwd;
)

基本上,我会很乐意用,只要是干净的任何解​​决方案。

Basically, I'll be happy with any solution as long as it's clean.

要澄清一下,我说的是一个更大的bash脚本这是一部分。其他人可能需要处理脚本下了线,所以我想保持它的清洁。我不希望有一条线,看起来像一个bash脚本:

To clarify, I'm talking about this being part of a larger bash script. Other people might need to deal with the script down the line, so I'd like to keep it clean. I don't want to have a bash script with one line that looks like:

ssh blah_server "ls some_folder; ./someaction.sh 'some params'; pwd; ./some_other_action 'other params';"

,因为它是非常丑陋,难以阅读。

because it is extremely ugly and difficult to read.

推荐答案

怎么样这里文件

ssh otherhost << EOF
  ls some_folder; 
  ./someaction.sh 'some params'
  pwd
  ./some_other_action 'other params'
EOF

要避免@Globalz在评论中提到的问题,您可能能够(取决于你正在做远程站点上的)与

To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with

ssh otherhost /bin/bash << EOF

这篇关于什么是干净的方式通过SSH和运行多个命令在Bash的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆