Search code examples
rubynet-ssh

Ruby net/ssh script not closing


I have a ruby script where I'm using net/ssh to ssh into a server, sudo -s, su - user, run a script and answer the questions to that script. So far I'm able to do everything, login, do all of the su/sudo stuff, run the script and answer its questions but the channel I create won't close and end the script, it seems. It all runs but then hangs after the script runs. What am I doing wrong? I'm a noob at ruby so I'm not totally sure what's going on. Thanks for any help!

Below is what I've got:

Net::SSH.start("server01", 'user') do |ssh|
  ssh.open_channel do |channel|
    channel.on_request "exit-status" do |channel, data|
      $exit_status = data.read_long
    end

    channel.on_data do |channel, data|
      data
    end

    channel.request_pty do |channel, data|
      channel.send_data("sudo -s\n")
      channel.send_data("su - user2\n")
      sleep 0.5

      channel.send_data("/opt/scripts/test\n")
      sleep 10

      channel.send_data("answer1\n")
      sleep 5

      channel.send_data("answer2\n")
      sleep 5

      channel.send_data("answer3\n")
      sleep 10
    end

    channel.wait
    puts "SUCCESS" if $exit_status == 0
  end
end

Solution

  • Actually in testing you need to send exit\n or else you will hang.

    so try adding it after your sleep (do you really need the sleep 10? )

     channel.send_data("answer3\n")
     sleep 10
     channel..send_data("exit\n")
    

    also sudo -u user2 -H /opt/scripts/test will make it so that you only have to exit the one shell. as your script as it stands starts 3 shells. 1 when you login and request a PTY, one when you run the root shell (sudo -s), and then another when you su over to user2.

    so the block would look something like this.

    channel.request_pty do |channel, data|
      channel.send_data("sudo -u user2 -H /opt/scripts/test")
      sleep 10
    
      channel.send_data("answer1\n")
      sleep 5
    
      channel.send_data("answer2\n")
      sleep 5
    
      channel.send_data("answer3\n")
      sleep 10
      channel.send_data("exit\n")
    
    end