English 中文(简体)
在巴什进行sh和进行多次指挥的最清洁方法是什么?
原标题:What is the cleanest way to ssh and run multiple commands in Bash?
  • 时间:2010-12-10 18:50:48
  •  标签:
  • bash
  • unix
  • ssh

我已经配备了短员,我可以在布什文字的外部服务器上进行指挥,如:

ssh blah_server "ls; pwd;"

现在,我真的要做的是,在外部服务器上有许多长期指挥。 在引标之间公布所有这些内容是相当令人怀疑的,而我确实避免了避免这种情况的多次。

因此,我能否在括号或部分内容中这样做? 我期望的是:

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

基本上,只要清理,我就乐于找到任何解决办法。

Edit

为了澄清,我谈论的是,这是更大双面文字的一部分。 其他人也许需要处理下行,因此我想保持清点。 我不想有一行的印本,像:

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

因为它极其难以阅读。

最佳回答

How about a Bash Here Document :

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

为了避免@Globalz在评论中提及的问题,你可以(视你在边远地点重新做什么)把第1行改为第1行。

ssh otherhost /bin/bash << EOF

请注意,您可在本文件中作可变的替代,但您可能必须处理引证问题。 例如,如果你引用“限制指示”(如EOF,则你可以不作变式替代。 但是,在不引用限额说明的情况下,可替代变量。 例如,如果你界定了<代码>$NAME。 以上是你手稿,你可以这样做。

ssh otherhost /bin/bash << EOF
touch "/tmp/${NAME}"
EOF

and it would create a file on the destination otherhost with the name of whatever you d assigned to $NAME. Other rules about shell script quoting also apply, but are too complicated to go into here.

问题回答

在当地打上你的手稿,然后把它推到sh,例如。

cat commands-to-execute-remotely.sh | ssh blah_server

commands-to-execute-remotely.sh 见上文:

ls some_folder
./someaction.sh
pwd;

为了与你的样本代码相符,你可以把你的指挥总结在单方或两面。 例如

ssh blah_server "
  ls
  pwd
"

我看到两种方式:

首先,你们要控制:

 ssh -oControlMaster=yes -oControlPath=~/.ssh/ssh-%r-%h-%p <yourip>

and run your commands

 ssh -oControlMaster=no -oControlPath=~/.ssh/ssh-%r-%h-%p <yourip> -t <yourcommand>

这样,你就可以在实际上不与服务器重新连接的情况下写短的指挥。

第二种是动态生成文字,scp,并运行。

This can also be done as follows. Put your commands in a script, let s name it commands-inc.sh

#!/bin/bash
ls some_folder
./someaction.sh
pwd

Save the file

如今,它正在边远服务器上运行。

ssh user@remote  bash -s  < /path/to/commands-inc.sh

Never failed for me.

将所有指挥部划入一个手稿,其操作方式可能与

ssh <remote-user>@<remote-host> "bash -s" <./remote-commands.sh

不能确定长期指挥的最清洁,但肯定是最容易的:

ssh user@host "cmd1; cmd2; cmd3"

这有利于制作文字,因为你不需要包括其他档案:

#!/bin/bash
ssh <my_user>@<my_host> "bash -s" << EOF
    # here you just type all your commmands, as you can see, i.e.
    touch /tmp/test1;
    touch /tmp/test2;
    touch /tmp/test3;
EOF

# you can use  $(which bash) -s  instead of my "bash -s" as well
# but bash is usually being found in a standard location
# so for easier memorizing it i leave that out
# since i dont fat-finger my $PATH that bad so it cant even find /bin/bash ..

<<>SSH>和Rong多指挥部>。

护栏内有半殖民地的单独指挥,后来又回过来,所有这些指挥都pipe入了sh。 例如:

echo "df -k;uname -a" | ssh 192.168.79.134

Pseudo-terminal will not be allocated because stdin is not a terminal.
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       18274628 2546476  14799848  15% /
tmpfs             183620      72    183548   1% /dev/shm
/dev/sda1         297485   39074    243051  14% /boot
Linux newserv 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux

使用多线插图和多面印本张贴的答案对我来说并不可行。

  • Long multiline strings are hard to maintain.
  • Separate bash scripts do not maintain local variables.

在保持当地环境的同时,这样做是一种实用的方法,可以缩短和操作多个指挥。

LOCAL_VARIABLE=test

run_remote() {
    echo "$LOCAL_VARIABLE"
    ls some_folder; 
    ./someaction.sh  some params 
    ./some_other_action  other params 
}

ssh otherhost "$(set); run_remote"

对像我这样坐在座的所有人来说,我成功地放弃了半殖民地和新线:

第一步:半殖民地。 这样,我们就不会打破 command子的指挥:

ssh <host> echo test;ls
                    ^ backslash!

Listed the remote hosts /home directory (logged in as root), whereas

ssh <host> echo test;ls
                    ^ NO backslash

现有工作名录。

下一步:打破这条线:

                      v another backslash!
ssh <host> echo test;
ls

这再次列出了远程工作名录——改进格式:

ssh <host>
  echo test;
  ls

如果真的比本记录更糟,或引自破碎的界线,那么我就不是要决定。

(Using bash, 乌班图14.04 LTS)

conf清你的系统,不停地用多条条条条条块,最容易的方法。

This can be done by creating a folder for the sockets:

mkdir ~/.ssh/controlmasters

之后,在您的组合中增加以下内容:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/controlmasters/%r@%h:%p.socket
    ControlPersist 10m

现在,你不需要修改你的任何法典。 这使多条电话能够在不设立多场会议的情况下打碎和打碎,如果需要增加当地和遥远的机器之间的相互作用,那是有用的。

http://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-multiplexing-to-speed-up-ssh-jointions/“rel=” http://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-multiplexing-to-speed-up-ssh-jointions/和

仅限指挥:

ssh <ssh_args> command1  &&  command2

ssh <ssh_args> command1 && command2

在巴什进行sh和进行多次指挥的最清洁方法是什么?

我建议利用这一职能。 这项职能有一条理由,即可以逃脱。 然后,sshqfunc输出declare -f of the function,然后将说明功能的插图以$@,并适当引用。 全文见%q” 引述,bash -c。 如果遥远者没有现金,则你可以改变ash。

sshqfunc() { echo "bash -c $(printf "%q" "$(declare -f "$@"); $1 "$@"")"; };

然后,根据你希望就边远开展的工作确定一项职能。 职能通常界定,因此将适当“明确”。 你可以在当地测试这种功能。 在界定后,适当的越权功能被传至边远。

work() {
   ls
   pwd
   echo "Some other command"
}

ssh host@something "$(sshqfunc work)"

通行证 你也可以提出论据,并将作为立场论点转达你。 该职能之后的下一个论点将分配给<代码>0——通常如<代码>-或_的持地人,用于将论点与呼吁分开。

work() {
   file=$1
   num=$2
   ls "$file"
   echo "num is $num"
}

ssh host@something "$(sshqfunc work)" -- /this/file 5

但指出,如果存在任何魔术性质,也应当适当引用论点:

ssh host@something "$(sshqfunc work)" -- "$(printf "%q" "$var1" "$var2")"




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签