Skip to content
This repository

After using ssh.sftp, ssh channel/exec hangs. #13

Open
ryanstout opened this Issue May 30, 2012 · 1 comment

2 participants

Ryan Stout Delano Mandelbaum
Ryan Stout

I've attached some sample code. I'm trying to get the exit status of commands, and also use the same connection to upload a file. You can see in the example that the command runs fine until I've used .sftp from the ssh connection. Also, if I do this after the sftp call it works:

connection.sftp.close_channel()
connection.instance_variable_set('@sftp', nil)

I'm thinking there's a way to keep both channels open (for the sftp and the command) and still have it work.

Thanks for the great library. Let me know if I can help track this one down.

require 'net/ssh'
require 'net/sftp'


class TestSSH
  def run_command(connection, command)
    exit_code = nil

    connection.open_channel do |channel|
      channel.exec(command) do |ch, success|
        unless success
          abort "FAILED: couldn't execute command (connection.channel.exec)"
        end

        channel.on_request("exit-status") do |ch,data|
          exit_code = data.read_long
        end
      end
    end
    puts "Loop on connection"
    connection.loop
    puts "Finished Loop"

    return exit_code
  end

  def test
    connection = Net::SSH.start('44dates.com', 'ubuntu')

    puts "Run ls"
    run_command(connection, "ls")

    puts "Upload File"
    io = StringIO.new("some text")
    connection.sftp.upload!(io, "somefile.txt")

    puts "Run ls Again"
    run_command(connection, "ls")
    puts "Finished Running ls again"

    connection.close
  end
end

TestSSH.new.test
Delano Mandelbaum
Collaborator

Thanks for the report. The project is in maintenance mode but I'm happy to pull in a patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.