问题
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