【问题标题】:How to run script on 200 servers and print output in bastion instance terminal? My bash script hangs on ssh to remote server如何在 200 台服务器上运行脚本并在堡垒实例终端中打印输出?我的 bash 脚本挂在 ssh 到远程服务器上
【发布时间】:2021-06-12 02:23:07
【问题描述】:

我们的服务器在 AWS (ec2) 上,我必须在 200 台服务器上运行相同的命令集,并且需要在我的终端上输出。我有可以访问所有 200 台服务器的堡垒实例。这个问题最初是在 2015 年提出的。 我有一个在机架空间上运行良好的脚本,但在 AWS 上,默认用户是 ec2-user,所以我需要根据 ec2-user 调整脚本或在 ssh 期间切换到 root 用户。

是否有任何语法问题或缺少什么? 我的脚本名称是 runOnAppServer.sh,然后是包含所有 ips 的文件,然后是前面提到的命令文件 --> runOnAppServer.sh server_ip scriptFile

下面会提到脚本

#!/bin/bash
# The private key used to identify this machine

IDENTITY_KEY=/home/sshKey.pem

syntax()
{
    echo "Syntax: runOnAppServer.sh server_ip scriptFile]"
    echo "For example: ./runOnAppServer.sh server_ip scriptFile"
    exit 1
}

if [ $# -ne 2 ]
then
    echo not enough arguments
    syntax
fi

echo "Running script $2 on $1"
ssh -t -t  -i $IDENTITY_KEY ec2-user@$1 sudo -i 'bash -s' < $2
exit
EOT

echo "Done"

【问题讨论】:

  • 你的脚本是否告诉它退出?
  • 不,它显示远程访问,我必须手动输入才能注销
  • 尝试在你的 ssh 命令中使用 logout 命令

标签: linux bash amazon-web-services shell amazon-ec2


【解决方案1】:

因为这就是您的脚本所做的。它在ssh 上运行sudo -i 'bash -s',没有参数,这会导致远程Bash 启动交互式shell,并等待您的输入。您正在指示 ssh 读取来自 $2 的输入,但它不起作用。

exitEOT 在技术上是语法错误,是的。看来您希望 ssh 命令以某种方式读取正在运行它的脚本的以下行,但它也不能那样工作。

你似乎在寻找类似Execute local script on remote Linux host的东西

【讨论】:

  • 带有合适的参数
【解决方案2】:

这不是我讨论过的重复问题或问题

我不想使用 scp,我想避免 scp 将文件复制到远程服务器然后执行,我使用 bash 脚本直接从我的服务器执行命令到远程服务器,通过这种方式,我正在运行命令在 100 台服务器上,当时没有合适的工具可以做到这一点。现在 AWS 正在为您提供 SSM 等。

答案:

The reason my script hangs while running a script was, I was using sudo -i and 
then 'bash -s', so what I need to do is, I just need to remove sudo -i 'bash -s' 
and add sudo in front of all commands that I am try to pass using file $2 from 
outside the file using another file.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2023-03-26
    • 2016-11-27
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多