def open_or_get_all(sftp, open_dir, local_dir)
handle = sftp.opendir(open_dir)
items = sftp.readdir(handle)
items.each do |item|
if item.filename != ‘.’ && item.filename != ‘…’
if item.longname[0…1] == ‘d’
# mkdir locally
Dir.mkdir(local_dir + item.filename, 0777)
# open dir and download all
open_or_get_all(sftp, open_dir + item.filename + ‘/’, local_dir
item.filename + ‘/’)
else #puts local_dir+item.filename #puts open_dir+item.filename
sftp.get_file open_dir+item.filename, local_dir+item.filename
end
end
end
sftp.close_handle(handle)
end
Net::SFTP.start(‘ext_server’, ‘user_name’, ‘password’) do |sftp|
open_or_get_all(sftp, “/home/user_name/dir_to_dnld/”, “/lcl_dir/”)
end