使用WinSCP .NET程序集将WinSCP脚本转换为C#代码

时间:2015-01-19 14:44:52

标签: winscp winscp-net

我已经使用WinSCP从外部服务器下载文件超过一年,方法是在每日任务运行时从我的C#代码中发出外部脚本。

我最近升级到版本5.5.6(来自V4.3.5.1463),这样我就可以使用.NET库,并为新的SFTP任务使用不同的服务器。新服务器上的SFTP任务使用.NET库非常有效。我对它非常满意,但旧的脚本代码现在失败了。

我尝试添加.NET代码只是为了登录并获取文件列表,但也失败了。但是,如果我直接运行winscp UI,我可以在服务器上看到这些文件并下载它们。我在代码中使用了与在UI中相同的细节,包括密钥。代码显然是连接到服务器并且密钥已确认,但随后无法进行身份验证!有人可以帮忙吗?

注意:我已将winscp.exe复制到与.NET库相同的文件夹中。我的帐户不需要密码。

我还创建了一个日志文件,如下......

注意,为了安全起见,在此日志中,我更改了用户名,连接IP地址和密钥。

. 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
. 2015-01-19 14:24:00.757 WinSCP Version 5.5.6 (Build 4746) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Standard)
. 2015-01-19 14:24:00.757 Configuration: nul
. 2015-01-19 14:24:00.757 Local account: SERVER\admin
. 2015-01-19 14:24:00.757 Working directory: C:\Users\admin\Desktop\Debug\Debug
. 2015-01-19 14:24:00.757 Process ID: 4444
. 2015-01-19 14:24:00.757 Command-line: "C:\Users\admin\Desktop\Debug\Debug\winscp.exe" /xmllog="C:\Users\admin\AppData\Local\Temp

\2\wscp0B6C.01408C35.tmp" /xmlgroups /nointeractiveinput /dotnet=556  /ini=nul /log="C:\mylet\website\Temp\WinScpLog.txt"  /console 

/consoleinstance=_2924_54848996_804
. 2015-01-19 14:24:00.757 Time zone: Current: GMT+0, Standard: GMT+0 (GMT Standard Time), DST: GMT+1 (GMT Daylight Time), DST Start: 

29/03/2015, DST End: 25/10/2015
. 2015-01-19 14:24:00.757 Login time: 19 January 2015 14:24:00
. 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
. 2015-01-19 14:24:00.757 Script: Retrospectively logging previous script records:
> 2015-01-19 14:24:00.757 Script: option batch on
< 2015-01-19 14:24:00.757 Script: batch           on        
> 2015-01-19 14:24:00.757 Script: option confirm off
< 2015-01-19 14:24:00.757 Script: confirm         off       
> 2015-01-19 14:24:00.757 Script: open -hostkey="ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa" -timeout=15 

"sftp://User@1.1.1.1:22"
. 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
. 2015-01-19 14:24:00.757 Session name: user@1.1.1.1 (Ad-Hoc site)
. 2015-01-19 14:24:00.757 Host name: 1.1.1.1 (Port: 22)
. 2015-01-19 14:24:00.757 User name: user (Password: No, Key file: No)
. 2015-01-19 14:24:00.757 Tunnel: No
. 2015-01-19 14:24:00.757 Transfer Protocol: SFTP
. 2015-01-19 14:24:00.757 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2015-01-19 14:24:00.757 Proxy: none
. 2015-01-19 14:24:00.757 Send buffer: 262144
. 2015-01-19 14:24:00.757 SSH protocol version: 2; Compression: No
. 2015-01-19 14:24:00.757 Bypass authentication: No
. 2015-01-19 14:24:00.757 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2015-01-19 14:24:00.757 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2015-01-19 14:24:00.757 SSH Bugs: A,A,A,A,A,A,A,A,A,A
. 2015-01-19 14:24:00.757 Simple channel: Yes
. 2015-01-19 14:24:00.757 Return code variable: Autodetect; Lookup user groups: A
. 2015-01-19 14:24:00.757 Shell: default
. 2015-01-19 14:24:00.757 EOL: 0, UTF: 2
. 2015-01-19 14:24:00.757 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2015-01-19 14:24:00.757 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2015-01-19 14:24:00.757 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2015-01-19 14:24:00.757 Cache directory changes: Yes, Permanent: Yes
. 2015-01-19 14:24:00.757 DST mode: 1; Timezone offset: 0h 0m
. 2015-01-19 14:24:00.757 --------------------------------------------------------------------------
. 2015-01-19 14:24:00.757 Looking up host "1.1.1.1"
. 2015-01-19 14:24:00.757 Connecting to 1.1.1.1 port 22
. 2015-01-19 14:24:00.804 Server version: SSH-2.0-0.0
. 2015-01-19 14:24:00.804 Using SSH protocol version 2
. 2015-01-19 14:24:00.804 We claim version: SSH-2.0-WinSCP_release_5.5.6
. 2015-01-19 14:24:00.819 Doing Diffie-Hellman group exchange
. 2015-01-19 14:24:01.053 Doing Diffie-Hellman key exchange with hash SHA-1
. 2015-01-19 14:24:01.365 Verifying host key dss 0x91486c77af8989ad 094f4a206efd983a ceeaae78321795b5 db1bf3a553007097 e1c8a86c09b41f62 

d93ca9af3dc8b5bd c10b6effc00da737 460fd7cea6c9491c 805ed7e13fbd423f 6ec239a74e66c8ff 16a199166f7076d8 f4e2874eeaf66f6c 6ba4d53436e4fed4 

68f44d7062052513 51f4eefc6e64c9f9 42e1fd1dc4c693f1 ,0x952e1dd3f13c9c9c e5d3fe2c7c96c137 173db4c9,0x88fc0112369215b4 0f4c670c3801f5e7 

08cbe3a8110c33f4 d6e6bf4880e8baa0 b1f0064f3808ebbc 286727e38bd4737f 32a67cb19b2851cc 58a87fd7166fdd0d 2524be55588b40a2 f78bba20c6db1049 

5b36125b0a1a9c94 31ce3c64a23cd028 94624932d350bb4c 056649e7ba10a807 f82037d5a4993340 3f511e923d1e031,0x2499ade72a6348f8 0919ccb5e56c68fd 

297191be353b28b7 4a7b5d55d461486d 8b86d0696617a74a a4055e1cf15baa2e 54afbb40223a6f7e 4500d7649bf51410 39a84eb81023d550 4bbd7cea6d4eb8f7 

8244bd3e8fa48387 90390b3cf3dd60a7 c45a6ddd967a8165 cf01da8309042e84 62eb7d511dd00348 ce127fdbc371d3c5  with fingerprint ssh-dss 1024 

92:ee:83:c2:30:33:de:f4:51:f2:c9:3f:ae:cd:91:9c
. 2015-01-19 14:24:01.365 Host key matches configured key
. 2015-01-19 14:24:01.365 Host key fingerprint is:
. 2015-01-19 14:24:01.365 ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa
. 2015-01-19 14:24:01.365 Initialised AES-256 SDCTR client->server encryption
. 2015-01-19 14:24:01.365 Initialised HMAC-SHA1 client->server MAC algorithm
. 2015-01-19 14:24:01.365 Initialised AES-256 SDCTR server->client encryption
. 2015-01-19 14:24:01.365 Initialised HMAC-SHA1 server->client MAC algorithm
! 2015-01-19 14:24:01.553 Using username "user".
. 2015-01-19 14:24:01.584 Prompt (7, SSH password, , &Password: )
. 2015-01-19 14:24:01.584 Disconnected: Unable to authenticate

这是我的C#代码:

SessionOptions sessionOptions = new SessionOptions
{
   Protocol = Protocol.Sftp,
   HostName = "1.1.1.1",
   PortNumber = 22,
   UserName = "user",
   SshHostKeyFingerprint = "ssh-dss 1024 aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa"
};

using (Session session = new Session())
{
   session.Open(sessionOptions); // the failure happens on this line
}

2 个答案:

答案 0 :(得分:1)

非常感谢Martin提醒我,我最初使用我存储在文件中的私钥设置了对此服务器的调用,并且必须设置WinSCP UI才能查看它(I&#39;忘了)。

我在磁盘上找到了该文件,并在SessionOptions

中添加了该文件的链接
SessionOptions sessionOptions = new SessionOptions
{
    ...
    SshPrivateKeyPath = @"C:\Files\myPrivateKey.ppk"
}

我的dotnet代码现在可以使用了,所以我要删除我的脚本并使用更整洁的dotnet代码,就像我打算对此更新一样。

答案 1 :(得分:0)

很难说,与4.3.5有什么不同,因为你没有从这个版本共享一个日志,也不是你的原始脚本。

无论如何,如果我正确理解你的帖子,你在WinSCP GUI中使用公钥认证。但是.NET代码中没有指定密钥,原因可能是,验证失败的原因(没有指定私钥或密码,因此无法进行身份验证)。

确保使用SessionOptions.SshPrivateKeyPath设置私钥文件的路径。

关于“密钥已确认”。这是服务器的公钥。确保understand the difference之间的server's key pair用于验证服务器的身份,your key pair用于身份验证。