脚本在ssh上挂起到远程服务器

时间:2015-03-12 23:16:54

标签: linux bash shell

我在远程服务器上使用具有给定IP的脚本运行命令,但是当脚本显示输出时,它会卡在远程服务器上,即它不会注销。

是否存在语法问题或遗漏?

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

IDENTITY_KEY=/home/admaximnew.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"

2 个答案:

答案 0 :(得分:0)

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

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

您似乎正在寻找类似Execute local script on remote Linux host

的内容

答案 1 :(得分:0)

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

我不想使用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.
相关问题