SSH host key fingerprint … does not match pattern … when using WinSCP .NET assembly in C# to download files

烂漫一生 提交于 2019-12-21 23:00:38

问题


I'm writing a C# app (want to eventually create a Windows service if I can get this working) to download files from a server using SCP (I am limited to SCP as the only option). I am using the WinSCP .NET assembly (v 5.15.3).

I have an SSH tunnel between my PC and the server, so the following works from the command line:

ssh user:server

I'm running in to a problem when setting the SshHostKeyFingerprint in my code.

WinSCP.SessionOptions options = new WinSCP.SessionOptions();

options.HostName = "ip address";
options.Password = "password";
options.PortNumber = 22;
options.UserName = "username";
options.Protocol = WinSCP.Protocol.Scp;
options.SshHostKeyFingerprint = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCkVu2A3SLLdTulOQ1XyGY......"; //full ssh key    

I've shortened the SSH key for easier reading.

When assigning the SshHostKeyFingerprint I get the following error:

SSH host key fingerprint "ssh-rsa 2048 AAAAB3NzaC1yc2EAA......" does not match pattern /((ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-nistp(256|384|521))( |-))?(\d+ )?(([0-9a-f]{2}(:|-)){15}[0-9a-f]{2}|[0-9a-zA-Z+/]{43}=)(;((ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-nistp(256|384|521))( |-))?(\d+ )?(([0-9a-f]{2}(:|-)){15}[0-9a-f]{2}|[0-9a-zA-Z+/]{43}=))*/

I guess I'm assigning a wrong SSH key (I have tried both client and server public keys). Can anyone point me in the right direction? Thanks.

Note: I did try doing this with a batch file but I couldn't run that on a schedule without the server being logged in.


回答1:


What should go to SessionOptions.SshHostKey**Fingerprint** is a fingerprint of the SSH host key. You are using a full host key (what you even say in the comment).

See also WinSCP FAQ Where do I get SSH host key fingerprint to authorize the server?


WinSCP GUI can generate a correct code template for you, including the fingerprint (assuming you have logged in at least once):



来源:https://stackoverflow.com/questions/57204629/ssh-host-key-fingerprint-does-not-match-pattern-when-using-winscp-net-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!