使用ScpClient连接ESXi 6.0并在c#中传输文件



我要连接Esxi 6.0管理程序并上传文件。我使用SSH网络从这个主题。

        using (var scp = new ScpClient("10.8.58.26", 22, "root", "MyPasword"))
        {
            scp.Connect();
        }

但是我得到"没有找到合适的身份验证方法来完成身份验证。"例外。虚拟化环境的SSH状态为"on",可以使用putty或winscp手动连接。我用linux尝试了这个,它正在工作。我应该如何正确地验证到esxi?

进一步的研究告诉我:"ESXi没有使用像众所周知的openssh那样功能齐全的ssh服务器。相反,它使用了内置在Busybox中的轻量级版本。"

我自己想出来的。我使用的是winscp库,而不是scp。代码如下所示:

SessionOptions sessionOptions= new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "xxx.xxx.xxx.xxx",
    UserName = "root",
    Password = "MyPasword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
};
private void WinScp(SessionOptions sessionOptions, string sourceFilePath, string destinationFilePath)
        {
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
                //// Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
                TransferOperationResult transferResult;
                transferResult = session.PutFiles(sourceFilePath, destinationFilePath, true, transferOptions);
                //// Throw on any error
                transferResult.Check();
            }
        }

SshHostKeyFingerprint可以在ESXi的"自定义系统/查看日志"->"查看支持信息"中找到。

最新更新