问题
I'm trying to use Renci SSH.NET to connect to an FTP site and download files from my C# app. I can successfully connect to the host with FileZilla and WinSCP (on Windows 10 Pro 64), and through an HTTP connection (the host is running CrushFTP), but cannot figure out how to successfully connect from my C# app, using SSH.NET. I'm getting this error:
The server response contains a null character at position 0x00000004: 00000000 15 03 03 00....A server must not send a null character before the Protocol Version Exchange is complete.
My take on the documentation from here, is that this problem appears to be an expected error. I've read the information suggested by the documentation at https://tools.ietf.org/html/rfc4253#section-4.2 and I "think" I understand that to mean that the host that I'm trying to connect to may not be correctly configured. My question is, since I don't have the ability to change how the host is configured, whether there is a way around this error with the SSH.NET library or am I just at a dead end for using SSH.NET?
Here's my code - tried to simplify to bare minimum:
using System;
using Renci.SshNet;
namespace mysftp
{
class Program
{
static void Main(string[] args)
{
string host = "myftps.hostname.com";
string username = "myusername";
string password = "securepassword";
int port = 990;
string remoteFolder = "/";
using (SftpClient sftp = new SftpClient(host, port, username, password))
{
try
{
Console.WriteLine("Attempting to connect to " + host + " ...");
sftp.Connect();
Console.WriteLine(sftp.ConnectionInfo.ServerVersion);
sftp.Disconnect();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
}
Here is the host information (Remote server info from WinSCP):
Remote system = UNIX Type: L8
File transfer protocol = FTP
Cryptographic protocol = TLS/SSL Implicit encryption, TLSv1.2
Encryption algorithm = TLSv1.2: ECDHE-RSA-AES256-GCM-SHA384, 2048 bit RSA, ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=RSA Enc=AESGCM(256) Mac=AEAD
Compression = No
------------------------------------------------------------
Certificate fingerprint
<<fingerprint info redacted>>
------------------------------------------------------------
Can change permissions = Yes
Can change owner/group = No
Can execute arbitrary command = Protocol commands only
Can create symbolic/hard link = No/No
Can lookup user groups = No
Can duplicate remote files = No
Can check available space = No
Can calculate file checksum = Yes
Native text (ASCII) mode transfers = No
------------------------------------------------------------
Additional information
The server supports these FTP additional features:
AUTH TLS
AUTH SSL
SSCN
PBSZ
PROT
CCC
CLNT
EPSV
EPRT
MDTM
MDTM YYYYMMDDHHMMSS[+-TZ];filename
MFMT
SIZE
REST STREAM
MODE Z
LIST -Q
SITE UTIME
SITE MD5
XMD5
SITE MD5s
SITE RANDOMACCESS
MLST Type*;Size*;Modify*;Perm*;UNIX.owner*;UNIX.group*;
UTF8
回答1:
Port 990 is used for FTP protocol over implicitly encrypted TLS/SSL connection (aka implicit FTPS). While SSH.NET is an SSH/SFTP library. FTPS and SFTP are completely different things. You cannot use SSH.NET for FTPS.
The implicit FTPS is obsolete thing. Not all FTP(S) libraries do support it. It's notable not supported by the built-in .NET FTP implementation, the FtpWebRequest
.
For you will have to use a 3rd party library. See Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?
Though most FTP servers do support the standard explicit FTPS (over port 21). If your server does, use that, instead of the legacy port 990. See FTPS (FTP over SSL) in C#
If not, talk to the server administrator to enable it.
See also Difference between FTP/FTPS/SFTP - Configurable connection to any of them
A completely different problem that can lead to the same error message: Connecting with SSH.NET to OpenSSH 7.4p1 fails with "The server response contains a null character at position" but works in WinSCP.
来源:https://stackoverflow.com/questions/65764847/the-server-response-contains-a-null-character-when-connecting-to-ftp-site-on-p