如何在 200 台服务器上 运行 脚本并在堡垒实例终端中打印输出?我的 bash 脚本挂在 ssh 到远程服务器上
How to run script on 200 servers and print output in bastion instance terminal? My bash script hangs on ssh to remote server
我们的服务器在 AWS (ec2) 上,我必须 运行 在 200 台服务器上执行相同的命令集,并且需要在我的终端上输出。我有堡垒实例,可以访问所有 200 台服务器。这个问题最初是在 2015 年提出的。
我有一个脚本,在 rackspace 上 运行ning 很好,但在 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 on "
ssh -t -t -i $IDENTITY_KEY ec2-user@ sudo -i 'bash -s' <
exit
EOT
echo "Done"
因为这就是您的脚本所做的。它 运行s sudo -i 'bash -s'
没有超过 ssh
的参数,这会导致远程 Bash 启动交互式 shell,并等待您的输入。您正在指示 ssh
从 </code> 读取输入,但它不会那样工作。</p>
<p> <code>exit
和 EOT
是技术上的语法错误,是的。您似乎希望 ssh
命令以某种方式读取它所来自的脚本的以下行 运行,但它也不会那样工作。
您似乎在寻找类似 Execute local script on remote Linux host
的内容
这不是我讨论过的重复问题或问题
我不想使用 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 from
outside the file using another file.
我们的服务器在 AWS (ec2) 上,我必须 运行 在 200 台服务器上执行相同的命令集,并且需要在我的终端上输出。我有堡垒实例,可以访问所有 200 台服务器。这个问题最初是在 2015 年提出的。 我有一个脚本,在 rackspace 上 运行ning 很好,但在 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 on "
ssh -t -t -i $IDENTITY_KEY ec2-user@ sudo -i 'bash -s' <
exit
EOT
echo "Done"
因为这就是您的脚本所做的。它 运行s sudo -i 'bash -s'
没有超过 ssh
的参数,这会导致远程 Bash 启动交互式 shell,并等待您的输入。您正在指示 ssh
从 </code> 读取输入,但它不会那样工作。</p>
<p> <code>exit
和 EOT
是技术上的语法错误,是的。您似乎希望 ssh
命令以某种方式读取它所来自的脚本的以下行 运行,但它也不会那样工作。
您似乎在寻找类似 Execute local script on remote Linux host
的内容这不是我讨论过的重复问题或问题
我不想使用 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 from
outside the file using another file.