2012-12-13 33 views
0

我的环境配置为在节点Server(Rundeck服务器)和节点Target(节点)之间为用户 “master”远程Solaris主机)。使用Net :: SSH Ruby库来执行需要sudo su的远程命令 - 另一个用户

在目标上,我想执行脚本/app/acme/stopApp.sh与用户appmanager

通常,当我需要手动运行该脚本,我使用:

ssh [email protected] sudo su - appmanager 

或者干脆:

ssh -t [email protected] 'sudo su - appmanager' 

其作品,未经密码,最后运行(如AppManager的):

/app/acme/stopApp.sh 

但我无法弄清楚如何使用Net :: SSH重现这些步骤。 当我执行sudo su - appmanager,然后/app/acme/stopApp.sh, 我在做一个子shell,对吧?

require 'rubygems' 
require 'net/ssh' 
require 'net/scp' 
require 'crypt/blowfish' 
require 'yaml' 

# 
# ... 
# 

Net::SSH.start(host, user, :password => password) do |session| 

    # It's possible to proceed in this way? 
    cmd = 'sudo su - appmanager;/app/acme/stopApp.sh' 
    ses = session.exec!(cmd) 

end 

我意识到,如果我试图像我在目标服务器上执行的东西:

sudo su -c /app/acme/stopApp.sh appmanager 

我收到以下消息:

We trust you have received the usual lecture from the local System Administrator. 

It usually boils down to these three things: 

#1) Respect the privacy of others. 
#2) Think before you type. 
#3) With great power comes great responsibility. Password: 

Password: 
+0

感谢Tin Man对文字的改进。我不是以英语为母语的人,所以你的行为可以帮助我更多地了解这门语言。 – Bera

回答

1

这是一个有点一个系统管理员的答案,但我认为你是两次验证:一次登录为“主人”(使用主人的密钥对),然后第二次“主人”su将su设为“appmanager”,但使用密码(因此“讲座”消息)。但我认为你没有第二次回答密码挑战。有几种方法可以解决这个问题:

1)直接使用该帐户的密钥对作为应用程序管理员登录。如果您担心应用程序管理员帐户的安全性,则可以使用restrict ssh remote commands等。

2)作为主人,将一个二进制文件(not a script!)称为setuid作为“appmanager”,它只是简单地调用stopApp.sh脚本。一个example

3)在/ etc/sudoers中设置master所在的适当组为NOPASSWD

相关问题