简体   繁体   English

使用 ruby gem net-ssh-multi 同时在多台服务器上执行 sudo 命令

[英]Using the ruby gem net-ssh-multi to execute a sudo command on multiple servers at once

In a previous question I figured out how to start a password-authenticated ssh sessions on multiple servers to run a single command.上一个问题中,我想出了如何在多个服务器上启动经过密码验证的 ssh 会话以运行单个命令。 Now I need to be able to execute a "sudo" command.现在我需要能够执行“sudo”命令。 The problem is, that net-ssh-multi does not allocate a pseudo terminal (pty), which sudo needs to run, resulting in the following error:问题是,net-ssh-multi 没有分配 sudo 需要运行的伪终端(pty),导致以下错误:

[127.0.0.1: stderr] sudo: sorry, you must have a tty to run sudo [127.0.0.1: stderr] sudo: 抱歉,你必须有一个 tty 才能运行 sudo

According to the documentation , a pseudo-terminal can be allocated with a method call to a channel object, however, the following code does not work: it generates the "no tty" error above:根据文档,可以通过对通道 object 的方法调用来分配伪终端,但是,以下代码不起作用:它会生成上面的“no tty”错误:

require 'net/ssh'
require 'net/ssh/multi'

Net::SSH::Multi.start do |session|


  # define the servers we want to use
  my_ticket.servers.each do |session_server|
    session.use session_server , :user =>  user_name ,  \
                              :password => user_pass
  end


 # execute commands on all servers
  session.exec 'sudo ls /root' do |channel, stream, data|
   if data =~ /^\[sudo\] password for user:/
     channel.request_pty # <- problem must be here.
     channel.send_data user_pass
   end

  end

 # run the aggregated event loop
 session.loop
end

$ ruby --version $ ruby --版本

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] ruby 1.8.7(2008-08-11 补丁级别 72)[i386-cygwin]

Can you try something like this:你可以尝试这样的事情:

  channel.request_pty do |c, success|
    if success
      command = "sudo YOUR_COMMAND"
      c.exec(command) do |c, success|
        # Some processing
      end
    end
  end

In this case 'sudo' is inside.在这种情况下,“sudo”在里面。

You need to request a pty before running the command.您需要在运行命令之前请求一个 pty。

session.open_channel do |ch|
  ch.request_pty
  ch.exec "sudo ls /root"
end

Also you may remove the tty requeriment from /etc/sudoers.您也可以从 /etc/sudoers 中删除 tty 请求。 To do it run visudo and comment Defaults requiretty为此,请运行visudo并注释Defaults requiretty

This is what I wound up doing, thanks to @Christian and this wonderful Pastie :这就是我最终要做的事情,感谢@Christian 和这个美妙的 Pastie

Net::SSH::Multi.start do |session|

# define the servers we want to use
my_ticket.servers.each do |session_server|
  session.use session_server , :user =>  my_ticket.user_name ,  \
  :password => my_ticket.user_pass
end



session.open_channel do |channel|
channel.request_pty do |c, success|
  raise "could not request pty" unless success
  channel.exec   "sudo YOUR_COMMAND"
  channel.on_data do |c_, data|
  if data = /\[sudo\]/
  channel.send_data(@password + "\n")
  end  
   puts data

    end
   end
  end

# run the aggregated event loop
  session.loop
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM