A solution for public-keyed SFTP and Ruby

Hello,

Lately there has been some talking here on how to send files using scp. Some
people are doing just fine by calling scp with a pre-shared password, but
neither I like that approach nor does it work for my purpose, so I did a bit
of experimentation and found this code works:

···

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

class SSHAgent
  def initialize
        @agent_env = Hash.new
        agenthandle = IO.popen("/usr/bin/ssh-agent -s", "r")
        agenthandle.each_line do |line|
          if line.index("echo") == nil
                  line = line.slice(0..(line.index(';')-1))
                  key, value = line.chomp.split(/=/)
                  puts "Key = #{key}, Value = #{value}"
                  @agent_env[key] = value
          end
        end
  end
  def [](key)
        return @agent_env[key]
  end
end

agent = SSHAgent.new
ENV["SSH_AUTH_SOCK"] = agent["SSH_AUTH_SOCK"]
ENV["SSH_AGENT_PID"] = agent["SSH_AGENT_PID"]
system("/usr/bin/ssh-add")

Net::SSH.start( '192.168.1.12',
                :username=>'pgquiles',
                :compression_level=>0,
                :compression=>'none'
              ) do |session|

        session.sftp.connect do |sftp|
                sftp.put_file("bigvideo.avi", "bigvideo.avi")
        end
end

I was using a passwordless private key for this experiment. Should you want to
use a password-protected private key, you might want to set the DISPLAY and
SSH_ASKPASS environment variables or use a smartcard reader and the '-s
reader' parameter.

The SSHAgent class comes from http://paste.lisp.org/display/6912 (I don't know
who its author is or the license that code is released under)

Lastly, there is an undesired behaviour in Net::SFTP: when you use the
put_file method it loads the whole file in memory, therefore you need loads
of memory if you want to send big files. I've tried a naive fix (iterating
writes) but it didn't work (it complains about "no such file", I think it's a
channels-related issue). Jamis Buck, the author of Net::SFTP, told me
currently he has no time to fix this issue. In case you feel brave enough,
the offending method starts in line 202 in
net-sftp-1.1.0/lib/net/sftp/session.rb

--
Pau Garcia i Quiles
http://www.elpauer.org
(Due to the amount of work, I usually need 10 days to answer)