2014-09-10 80 views
2

对于我正在使用的服务器技术,可以通过运行一些命令行参数手动将服务器加入集合。在某一时刻,控制台会提示您是否要接受证书链,并输入(y/n)并且该命令一直运行。我试图自动化这个过程,但是我在响应输入提示时碰到了一个障碍,并且在四处探听之后听说它可能是一个SSL事件,所以我不知道是否有不同的方式来做到这一点。如何获取控制台输入以在bash脚本中使用SSL?

如果你做手工,这是它的外观:

Joining the collective with target controller ...this may take a while 
SSL trust has not been established with the target server 
//certificate chain 
Do you want to accept the above certificate chain? (y/n) 

然而,既:

echo "y y" | bash ./script.sh 

//inside script.sh 
echo "y y" | $(command) 

结束了这种反应:

Joining the collective with target controller ...this may take a while 
SSL trust has not been established with the target server 
//certificate chain 

Input console is not available 

Aborting join collective. 

Error: 
Unable to complete the MBean operation 
Error: java.securit.cert.CertificateException: User has rejected the request to trust the 
certificate chain 

我希望有人或许能够通过手动回应的方式来做到这一点

回答

4

错误Input console is not available表明目标程序期望与真实终端通信,而不是管道(如果您尝试将echo加入到程序中,您会得到这个结果)。

在这种情况下,您将使用类似expect的模拟真实终端(使用伪tty)的方式自动执行该程序,并且通常可以“欺骗”这样的程序,使其相信它们正在与实际的终端进行通信。 expect将让你输入任意命令到程序中。

有关如何使用Expect(此处用于自动执行ssh)的示例,请参阅Bash/Expect Script for SSH

+0

我看,我假设有这样做,则没有原生的方式?我的限制相当严格,我必须编写一个脚本,可以在“开箱即用”的机器上运行,谢谢你的解释,尽管 – sreya 2014-09-10 18:51:09

+0

不幸的是,没有。没有很好的,相对便携的使用我知道的纯bash使用ptys。 – nneonneo 2014-09-10 18:53:20

+0

或者远程命令可能不期望一个真正的终端,但你的SSH不分配tty。您可以使用选项“-t”强制执行伪tty分配 – mcoolive 2014-09-10 23:15:06

1

下面是一个基本expect例如,应该工作,每@nneonneo:

#!/usr/bin/expect 

set timeout 600 
spawn -noecho /path/to/script.sh 
expect { 
    (y/n) { 
    send -- "y\n" 
    exp_continue 
    } 
    timeout { 
    exit 1 
    } 
    eof { 
    catch wait result 
    exit [lindex $result 3] 
    } 
} 
0

我假设你正在使用WebSphere自由作为您的服务器技术,因为输出是我的安装了。反正你需要把关键参数是--autoAcceptCertificates

例如:

collective join defaultServer --host=controller \ 
    --port=9443 \ 
    --keystorePassword=memberPassword \ 
    --user=adminUser \ 
    --password=adminPassword \ 
    --autoAcceptCertificates 
相关问题