开发者

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. Now I need to be able to execute a "sudo" command. The problem is, that net-ssh-multi does not allocate a pseudo terminal (pty), which sudo needs to run, resulting in the following error:

[127.0.0.1 : stderr] sudo: sorry, you must have a tty to run 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:

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 sessi开发者_开发知识库on_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 1.8.7 (2008-08-11 patchlevel 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.


You need to request a pty before running the command.

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

Also you may remove the tty requeriment from /etc/sudoers. To do it run visudo and comment Defaults requiretty


This is what I wound up doing, thanks to @Christian and this wonderful 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜