NET::SSH sudo su problems

I’m trying to run the following code and having problems

Net::SSH.start(host, user) do |ssh|
ssh.exec!(“sudo su - qos”)

I run the main which calls this method, but it just hangs and doesnt
actually change the user to “qos”

I think the problem you’re having is that sudo requires a PTY otherwise
it
prompts for a password, which you experience as “hangs”.

Unfortunately, I couldn’t find a simple way to make Net::SSH enable PTY
(it’s in the ssh protocol), so I had to make my own exec function. As a
bonus, it prints the output buffer while running so you can observe this
PTY
behavior.

Good luck!

Tal

def exec(ssh, command, request_pty=false)
exit_code = 0
output_buffer = []
c = ssh.open_channel do |chan|
chan.on_request(‘exit-status’) do |ch, data|
exit_code += data.read_long
print(“ssh exec exit-status: #{exit_code}”)
end
chan.on_request(‘exit-signal’) do |ch, data|
exit_code += (data.read_long << 8)
print(“ssh exec exit-signal: #{exit_code}”)
end
chan.on_data do |ch, data|
output_buffer << data
print(“ssh data: #{data}”)
end
chan.on_extended_data do |ch, type, data|
output_buffer << data
print(“ssh extended data (#{type}): #{data}”)
end
chan.request_pty() if request_pty
chan.exec(command)
end
c.wait
output = output_buffer.join(’’)
print(“ssh “#{command}” exit_code=#{exit_code}
output=\n#{output}”)
return exit_code, output_buffer.join(’’)
end