From: Pau Garcia i Quiles Date: 2006-09-14T17:08:29+09:00 Subject: A solution for public-keyed SFTP and Ruby --nextPart2229850.6dS9ikaU80 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello, Lately there has been some talking here on how to send files using scp. Som= e=20 people are doing just fine by calling scp with a pre-shared password, but=20 neither I like that approach nor does it work for my purpose, so I did a bi= t=20 of experimentation and found this code works: =3D=3D=3D=3D=3D=3D=3D=3D=3D require 'net/ssh' require 'net/sftp' class SSHAgent def initialize @agent_env =3D Hash.new agenthandle =3D IO.popen("/usr/bin/ssh-agent -s", "r") agenthandle.each_line do |line| if line.index("echo") =3D=3D nil line =3D line.slice(0..(line.index(';')-1)) key, value =3D line.chomp.split(/=3D/) puts "Key =3D #{key}, Value =3D #{value}" @agent_env[key] =3D value end end end def [](key) return @agent_env[key] end end agent =3D SSHAgent.new ENV["SSH_AUTH_SOCK"] =3D agent["SSH_AUTH_SOCK"] ENV["SSH_AGENT_PID"] =3D agent["SSH_AGENT_PID"] system("/usr/bin/ssh-add") Net::SSH.start( '192.168.1.12', :username=3D>'pgquiles', :compression_level=3D>0, :compression=3D>'none' ) do |session| session.sftp.connect do |sftp| sftp.put_file("bigvideo.avi", "bigvideo.avi") end end =3D=3D=3D=3D=3D=3D=3D=3D=3D I was using a passwordless private key for this experiment. Should you want= to=20 use a password-protected private key, you might want to set the DISPLAY and= =20 SSH_ASKPASS environment variables or use a smartcard reader and the '-s=20 reader' parameter. The SSHAgent class comes from http://paste.lisp.org/display/6912 (I don't k= now=20 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=20 put_file method it loads the whole file in memory, therefore you need loads= =20 of memory if you want to send big files. I've tried a naive fix (iterating= =20 writes) but it didn't work (it complains about "no such file", I think it's= a=20 channels-related issue). Jamis Buck, the author of Net::SFTP, told me=20 currently he has no time to fix this issue. In case you feel brave enough,= =20 the offending method starts in line 202 in=20 net-sftp-1.1.0/lib/net/sftp/session.rb =2D-=20 Pau Garcia i Quiles http://www.elpauer.org (Due to the amount of work, I usually need 10 days to answer) --nextPart2229850.6dS9ikaU80 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQBFCQ24/DzYv9iGJzsRAiySAJkBHnnNd4uttl7qGu/vor1uLOg8uQCgo/XA 2djN3DHotoY5/Wx90womO00= =CljH -----END PGP SIGNATURE----- --nextPart2229850.6dS9ikaU80--