我已经设置了ssh代理,我能够在Bash脚本中运行外部服务器上的命令,例如: bash
ssh blah_server "ls; pwd;"
如今,我真正想作的是在外部服务器上运行不少长命令。 将全部这些包含在引号之间会很是难看,并且我宁愿屡次避免ssh'ing以免这种状况。 服务器
那么,有没有一种方法能够用括号括起来的一个方法作到这一点? 我正在寻找如下内容: ssh
ssh blah_server ( ls some_folder; ./someaction.sh; pwd; )
基本上,只要它干净,我会对任何解决方案感到满意。 spa
为了澄清,我在谈论这是一个更大的bash脚本的一部分。 其余人可能须要处理脚本,因此我想保持清洁。 我不但愿有一个bash脚本,其中一行看起来像: 代理
ssh blah_server "ls some_folder; ./someaction.sh 'some params'; pwd; ./some_other_action 'other params';"
由于它很是丑陋且难以阅读。 code
要匹配您的示例代码,您能够将命令包装在单个或双重qoutes中。 例如 server
ssh blah_server " ls pwd "
将全部命令放在脚本上,它能够像运行同样运行rem
ssh <remote-user>@<remote-host> "bash -s" <./remote-commands.sh
这也能够以下完成。 将命令放在脚本中,咱们将其命名为commands-inc.sh 字符串
#!/bin/bash ls some_folder ./someaction.sh pwd
保存文件 terminal
如今在远程服务器上运行它。
ssh user@remote 'bash -s' < /path/to/commands-inc.sh
从未对我失败过。
在Bash中SSH和运行多个命令。
用字符串中的分号分隔命令,传递给echo,全部命令都传递到ssh命令。 例如:
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
这适用于建立脚本,由于您没必要包含其余文件:
#!/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