0

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
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
Tony D
  • 49
  • 1
  • 7
  • not sure why it isn't closing, but why not `sudo -u user2 -H /opt/scripts/test` ? Should save a couple commands . also have you tried issuing the exit command to close the shell? – Doon Aug 29 '14 at 22:05

1 Answers1

0

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
Doon
  • 19,719
  • 3
  • 40
  • 44
  • I'll give it a shot. The -H shouldn't be there. I'm not sure why it's in there. – Tony D Aug 29 '14 at 22:25
  • The -H sets the HOMEDIR to the users home. You may or may not need it. – Doon Aug 29 '14 at 22:27
  • This got me in the right direction. I had to run sudo -s using a channel.exec. Using send_data, nothing happened, and I also had to do a channel.send_data("exit\n") twice. It ran and closed the connection. – Tony D Aug 31 '14 at 11:28
  • see my note about the sudo -s opening another shell, but if you use sudo -u user2 -H /opt/scripts/test it should run as user2, then close that shell when the script exits, and then the single exit should be all that was needed, but yes with the sudo -s you will need to exit all the shells, hence the extra exits – Doon Sep 01 '14 at 15:17