From: Woody Peterson Date: 2008-09-12T19:02:10+09:00 Subject: Re: using net::ssh shell to sudo to another user and execute commands All, While I don't have the specific reason your scripts are failing, I have a snippet from something I'm writing that might be handy. Note that 'yall are using Net::SSH v1, and my solution is for v2. def exec(cmd) # the built in ssh.exec wraps some stuff up for us, but to catch sudo we # have to construct the whole thing ourselves, starting with the channel. channel = ssh.open_channel do |channel| # now we request a "pty" (i.e. interactive) session so we can send data # back and forth if needed. it WILL NOT WORK without this, and it has to # be done before any call to exec. channel.request_pty do |ch, success| raise "Could not obtain pty (i.e. an interactive ssh session)" if !success end channel.exec(cmd) do |ch, success| # 'success' isn't related to bash exit codes or anything, but more # about ssh internals (i think... not bash related anyways). # not sure why it would fail at such a basic level, but it seems smart # to do something about it. abort "could not execute command" unless success # on_data is a hook that fires when the loop that this block is fired # in (see below) returns data. This is what we've been doing all this # for; now we can check to see if it's a password prompt, and # interactively return data if so (see request_pty above). channel.on_data do |ch, data| if data == "Password:" puts "Password request" channel.send_data "#{self.password}\n" else # ssh channels can be treated as a hash for the specific purpose of # getting values out of the block later channel[:result] ||= "" channel[:result] << data end end channel.on_extended_data do |ch, type, data| raise "SSH command returned on stderr: #{data}" end end end # Nothing has actually happened yet. Everything above will respond to the # server after each execution of the ssh loop until it has nothing left # to process. For example, if the above recieved a password challenge from # the server, ssh's exec loop would execute twice - once for the password, # then again after clearing the password (or twice more and exit if the # password was bad) channel.wait return channel[:result].strip # it returns with \r\n at the end end -- Posted via http://www.ruby-forum.com/.