perl using .Net with WinSCP and ListDirectory parameter per File 'FilePermissions'

Advertisement

jpantera
Joined:
Posts:
23
Location:
Los Angeles, CA

perl using .Net with WinSCP and ListDirectory parameter per File 'FilePermissions'

Hello,

Wanting to understand how the file permissions might be being returned, using Perl and the .Net WinSCP "ListDirectory" functionality.

I followed the examples for both Perl, and for PowerShell, and see how in PowerShell, after reading in the list of files from a remote SFTP site, this print (WriteHost) statement gives straight information I'm coveting in my Perl script:

foreach ($fileInfo in $directory.Files)
{
Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
"permissions $($fileInfo.FilePermissions) and " +
"last modification at $($fileInfo.LastWriteTime)")
}

Following the same logic, and parameters given, I can duplicate this with Perl, , using Win32::OLE .Net WinSCP method:

my $directory = $session->ListDirectory("/files/");
my $items = Win32::OLE::Enum->new($directory->{'Files'});

However, specifically, the value inside the loop of file objects for FilePermissions:

my $item;
while (defined($item = $items->Next))

for: $item->{'FilePermissions'}

Produces the following sort of output:
FilePermissions = <Win32::OLE=HASH(0x25cc0e0)>
FilePermissions = <Win32::OLE=HASH(0x25cc0f8)>
.
.
.

The same files, in the PowerShell output give:
FilePermissions = <rw-rw-r-->
FilePermissions = <rw-rw-r-->

I tried setting the value returned by 'FilePermissions' to a Hash, and palying around with, but having no luck getting the proper output (And am modifying a program that's written in Perl, so can't really simply go to PowerShell for all of it at this time).

Thank you,
Joe P.

Reply with quote

Advertisement

jpantera
Joined:
Posts:
23
Location:
Los Angeles, CA

Thank you

Thank you, Martin!

You rock. This solved (I looked at this, but needed your reminder to realize what had to be done :-) ).

It seems PowerShell defaulted to the Text property then? (there's a project downhill to re-write this in PowerShell).

Thanks for your assistance.

- Joe P.

Reply with quote

jpantera
Joined:
Posts:
23
Location:
Los Angeles, CA

PassPhrase Usage with the .Net?

Hello, Martin, thank you for the follow-up on all topics.
This may need a new topic, but the site is telling me in read-only mode (so adding here).

I have connections to sites using non-blank passphrases and .ssh public and private keys. I'm successful via the WinSCP GUI, and via the building a running script, and winscp.com

Is it still an issue with the .Net Assembly, to do this?

I've read your threads about two-phase authentication...

Via the GUI, the log shows:

. 2019-02-12 11:23:47.734 Reading key file "<PATH>\id_rsa.ppk"
! 2019-02-12 11:23:47.736 Using username "XXXXXXX".
. 2019-02-12 11:23:47.758 Waiting for the server to continue with the initialization
. 2019-02-12 11:23:47.758 Looking for incoming data
. 2019-02-12 11:23:47.758 Looking for network events
. 2019-02-12 11:23:47.873 Detected network event
. 2019-02-12 11:23:47.873 Enumerating network events for socket 2276
. 2019-02-12 11:23:47.873 Enumerated 1 network events making 1 cumulative events for socket 2276
. 2019-02-12 11:23:47.873 Handling network read event on socket 2276 with error 0
. 2019-02-12 11:23:47.873 Server offered these authentication methods: publickey,password,keyboard-interactive
. 2019-02-12 11:23:47.873 Offered public key
. 2019-02-12 11:23:47.874 Waiting for the server to continue with the initialization
. 2019-02-12 11:23:47.874 Looking for incoming data
. 2019-02-12 11:23:47.874 Looking for network events
. 2019-02-12 11:23:47.939 Detected network event
. 2019-02-12 11:23:47.939 Enumerating network events for socket 2276
. 2019-02-12 11:23:47.939 Enumerated 1 network events making 1 cumulative events for socket 2276
. 2019-02-12 11:23:47.939 Handling network read event on socket 2276 with error 0
. 2019-02-12 11:23:47.939 Offer of public key accepted
! 2019-02-12 11:23:47.939 Authenticating with public key "<KEY>"
. 2019-02-12 11:23:47.964 Prompt (passphrase, "SSH key passphrase", <no instructions>, "Passphrase for key "<KEY>": ")
. 2019-02-12 11:23:48.039 Session upkeep
. 2019-02-12 11:23:48.039 Looking for network events
. 2019-02-12 11:23:48.039 Timeout waiting for network events
. 2019-02-12 11:23:48.493 Session upkeep
. 2019-02-12 11:23:48.493 Looking for network events
. 2019-02-12 11:23:48.493 Timeout waiting for network events
. 2019-02-12 11:23:48.996 Session upkeep
. 2019-02-12 11:23:48.996 Looking for network events
. 2019-02-12 11:23:48.996 Timeout waiting for network events
. 2019-02-12 11:23:49.494 Session upkeep
. 2019-02-12 11:23:49.495 Looking for network events
. 2019-02-12 11:23:49.495 Timeout waiting for network events
. 2019-02-12 11:23:49.996 Session upkeep
. 2019-02-12 11:23:49.997 Looking for network events
. 2019-02-12 11:23:49.997 Timeout waiting for network events
. 2019-02-12 11:23:50.492 Session upkeep
. 2019-02-12 11:23:50.493 Looking for network events
. 2019-02-12 11:23:50.493 Timeout waiting for network events
. 2019-02-12 11:23:50.992 Session upkeep
. 2019-02-12 11:23:50.994 Looking for network events
. 2019-02-12 11:23:50.994 Timeout waiting for network events
. 2019-02-12 11:23:51.493 Session upkeep
. 2019-02-12 11:23:51.494 Looking for network events
. 2019-02-12 11:23:51.494 Timeout waiting for network events
. 2019-02-12 11:23:51.680 Sent public key signature
. 2019-02-12 11:23:51.680 Waiting for the server to continue with the initialization
. 2019-02-12 11:23:51.680 Looking for incoming data
. 2019-02-12 11:23:51.680 Looking for network events
. 2019-02-12 11:23:51.746 Detected network event
. 2019-02-12 11:23:51.746 Enumerating network events for socket 2276
. 2019-02-12 11:23:51.746 Enumerated 1 network events making 1 cumulative events for socket 2276
. 2019-02-12 11:23:51.746 Handling network read event on socket 2276 with error 0
. 2019-02-12 11:23:51.746 Access granted

In the .Net code, I set these variables (PowerShell or Perl):

PowerShell here:

# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "<SITE.SFTP.COM>"
UserName = "XXXXXXX"
SshPrivateKeyPath = "<PATH>\id_rsa.ppk"
PrivateKeyPassphrase = "<MY_KEY_HERE>"
SshHostKeyFingerprint = "<SSH KEY Fingerprint>"
}

$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\temp\winscp.log"

LOG FROM This (winscp.log) fails immediately, rejecting the key attempt:

. 2019-02-12 11:29:22.599 Reading key file "<PATH>\id_rsa.ppk"
! 2019-02-12 11:29:22.600 Using username "XXXXXXX".
. 2019-02-12 11:29:22.672 Server offered these authentication methods: publickey,password,keyboard-interactive
. 2019-02-12 11:29:22.672 Offered public key
! 2019-02-12 11:29:22.738 Server refused our key
. 2019-02-12 11:29:22.739 Server refused our key
. 2019-02-12 11:29:22.739 Server offered these authentication methods: publickey,password,keyboard-interactive
. 2019-02-12 11:29:22.739 Attempting keyboard-interactive authentication
. 2019-02-12 11:29:22.804 Server refused keyboard-interactive authentication
. 2019-02-12 11:29:22.804 Server offered these authentication methods: publickey,password,keyboard-interactive
. 2019-02-12 11:29:22.804 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2019-02-12 11:29:22.805 Disconnected: Unable to authenticate


What am I doing wrong? (or is this just not possible?)

- Joe P.

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum