Question

I have code that requires me to connect to one server, rsync to a different server, then connect to the second server and run a bunch of commands on it. But without fail, the second SSH connection throws a 'do_open_failed': open failed (1) (Net::SSH::ChannelOpenFailed) error. Am I doing something wrong here, is there a way to close the first connection properly that makes the second one connect?

Net::SSH.start(self.from_creds['host'], self.from_creds['user'], :password => self.from_creds['password']) do |ssh|
  channel = ssh.open_channel do |ch|
    ch.exec "/usr/bin/rsync -e ssh -varuzP --exclude=sys-export --delete #{self.from_creds['filepath']}/#{self.client_id}/ #{self.scp_to}/#{new_client_id}" do |ch, success|
      raise "could not execute command" unless success

      # "on_data" is called when the process writes something to stdout
      ch.on_data do |c, data|
        $stdout.print data
      end

      # "on_extended_data" is called when the process writes something to stderr
      ch.on_extended_data do |c, type, data|
        $stderr.print data
      end

      ch.on_close { puts "done!" }
    end
  end
  channel.wait
end
Net::SSH.start(self.to_creds['host'], self.to_creds['user'], :password => self.to_creds['password']) do |ssh1|
  # Do some other stuff here
  tmp_path = "#{self.to_creds['filepath']}/tmp/#{Time.now.to_i}"
  ssh1.exec "mkdir -p #{tmp_path}"
  ssh1.exec "cd #{self.to_creds['filepath']}/#{new_client_id}"
end
Was it helpful?

Solution

According to the documentation, exec doesn't block. Trying using exec! instead.

Net::SSH.start(self.to_creds['host'], self.to_creds['user'], :password => self.to_creds['password']) do |ssh1|
  # Do some other stuff here
  tmp_path = "#{self.to_creds['filepath']}/tmp/#{Time.now.to_i}"
  ssh1.exec! "mkdir -p #{tmp_path}"
  ssh1.exec! "cd #{self.to_creds['filepath']}/#{new_client_id}"
end

Alternatively,

Net::SSH.start(self.to_creds['host'], self.to_creds['user'], :password => self.to_creds['password']) do |ssh1|
  # Do some other stuff here
  tmp_path = "#{self.to_creds['filepath']}/tmp/#{Time.now.to_i}"
  ssh1.exec "mkdir -p #{tmp_path}"
  ssh1.exec "cd #{self.to_creds['filepath']}/#{new_client_id}"
  ssh1.loop
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top