通过 ssh 调用交互式 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11372960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling an interactive bash script over ssh
提问by Mario Aguilera
I'm writing a "tool" - a couple of bash scripts - that automate the installation and configuration on each server in a cluster.
我正在编写一个“工具” - 几个 bash 脚本 - 自动在集群中的每个服务器上进行安装和配置。
The "tool" runs from a primary server. It tars and distributes it's self (via SCP) to every other server and untars the copies via "batch" SSH.
“工具”从主服务器运行。它将它自己(通过 SCP)压缩并分发到每个其他服务器,并通过“批处理”SSH 解压缩副本。
During set-up the tool issues remote commands such as the following from the primary server: echo './run_audit.sh' | ssh host4 'bash -s'. The approach works in many cases, except when there's interactive behavior since standard input is already in use.
在设置过程中的工具的问题远程命令,例如从主服务器的情况如下:echo './run_audit.sh' | ssh host4 'bash -s'。该方法在许多情况下都有效,除非存在交互行为,因为标准输入已经在使用中。
Is there a way to run remote bash scripts interactively over SSH?
有没有办法通过 SSH 以交互方式运行远程 bash 脚本?
As a starting point, consider the following case: echo 'read -p "enter name:" name; echo "your name is $name"' | ssh host4 'bash -s'
作为起点,请考虑以下情况: echo 'read -p "enter name:" name; echo "your name is $name"' | ssh host4 'bash -s'
In the case above the prompt never happens, how do I work around that?
在上述情况下,提示永远不会发生,我该如何解决?
Thanks in advance.
提前致谢。
回答by dave4420
Run the command directly, like so:
直接运行命令,像这样:
ssh -t host4 bash ./run_audit.sh
For an encore, modify the shell script so it reads options from the command line or a configuration file instead of from stdin (or in preference to stdin).
对于 encore,修改 shell 脚本,使其从命令行或配置文件而不是从标准输入(或优先于标准输入)读取选项。
I second Dennis Williamson's suggestion to look into puppet/etc instead.
我支持丹尼斯·威廉姆森 (Dennis Williamson) 的建议,转而研究 puppet/etc。
回答by nudzo
Do not pipe commands via stdin to ssh, but copy shell script to remote machine:
不要通过 stdin 将命令通过管道传输到 ssh,而是将 shell 脚本复制到远程机器:
scp ./run_audit.sh host4:
and then:
进而:
ssh host4 run_audit.sh
For cluster deployments I'm using Fabric... it runs on top of SSH protocol, no daemons needed. It's easy as writing fabfile.py:
对于集群部署,我使用的是 Fabric……它运行在 SSH 协议之上,不需要守护进程。就像编写 fabfile.py 一样简单:
from fabric.api import run
def host_type():
run('uname -s')
and then:
进而:
$ fab -H localhost,linuxbox host_type
[localhost] run: uname -s
[localhost] out: Darwin
[linuxbox] run: uname -s
[linuxbox] out: Linux
Done.
Disconnecting from localhost... done.
Disconnecting from linuxbox... done.
Of course it can do more... including interactive commands, and relays on ~/.ssh directory files for SSH. More at fabfile.org. For sure you will forget bash for such tasks. ;-)
当然,它可以做更多...包括交互式命令,并为 SSH 中继 ~/.ssh 目录文件。更多信息请访问fabfile.org。您肯定会忘记 bash 执行此类任务。;-)

