Jason Nicholas
Jason Nicholas

Reputation: 93

Broken filenames with Ruby when connected to SFTP version 5

While building a new ruby automation script to handle files up and down from an sftp connection I am having trouble extracting filenames. Using these same exact methods I have built automated sftp scripts that are actively being used and have not had much trouble, but I can not seem to output the available filenames of files in this sftp setup.

I know the sftp connection is fine as I am able to upload files and can hardcode in path/filename to download files. My problem seems to be specifically with reading the names of the files.

Using typical methods, I can only seem to get output of what appears to be the file object or a mass of jumbled text which does include the filenames:

"#<Net::SFTP::Protocol::V04::Name:0x4f6d598 @name=nil, @attributes=#<Net::SFTP::Protocol::V04::Attributes:0x4f6d5c8 @attributes={:type=>1}>>"

or (clip from the mass of jumbled(octal?) data that does contain filenames):

"#<Net::SFTP::Protocol::V04::Name:0x4f6d778@name="inistrators@BUILTIN\000\000\000\027domain users@POSTNTRACK\000\000\001\200\000\000\000\000X\201\031)\000\000\000\000XE\252\312\000\000\000\000X\201\031)\000\000\000\b\000\000\000\031MVP20170106-238682954.999\000\000\002\275\001\000\000\000\000\000\000\001K\000\000\000\026Administrators@BUILTIN\000\000\000\027domain users@POSTNTRACK\000\000\001\200\000\000\000\000X~z\366\000\000\000\000X~z\366\000\000\000\000Xo\373=\000\000\000\000\000\000\000'MVP20170106-238682954.HTML\000\000\002\275\001\000...."

This output is from the code(simplified):

require 'net/sftp'
require 'net/ssh'
Net::SSH.start(@site,@user,:password =>@pass) do |session|
session.sftp.connect do |sftp|
sftp.dir.foreach('/Download') do |file|
next if (file.name == '..' || file.name == '.')
next if !file.file?
p file

This should be simple but I have been working on this problem for weeks; please help provide info, point out where I am going wrong, or could this be an issue on the SFTP side? Have tried with and without SSH session included

Upvotes: 2

Views: 349

Answers (2)

David Moles
David Moles

Reputation: 51113

Net::SFTP 2.1.3rc1 and later (including 3.0.0, released in 2020) support passing an SFTP version as an option to Net::SFTP#start:

ssh_opts = { password: @pass }
sftp_opts = { version: 3 }

Net::SFTP.start(@site, @user, ssh_opts, sftp_opts) do |sftp|
  sftp.dir.foreach('/Download') do |file|
    # ...
  end
end

Upvotes: 1

Martin Prikryl
Martin Prikryl

Reputation: 202282

The server implements SFTP version 5. That's rather unusual. Most SFTP servers (OpenSSH in particular) support version 3 only. For that reason newer versions of the protocol are rarely used and it's not uncommon that their implementation is buggy (as not properly tested). What seems to be the case here. The Ruby implementation of the SFTP version 5 is probably wrong.

You can force lower version of the protocol using HIGHEST_PROTOCOL_VERSION_SUPPORTED=3 in sftp/session.rb. It does not seem that there's a way to change the version on a per-session basis. Though, it's unlikely that the change breaks your other sessions, as they most probably use the version 3 anyway.

Upvotes: 1

Related Questions