C# 使用 Tamir.SharpSsh 的 SSH/SFTP 连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1579333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:00:44  来源:igfitidea点击:

SSH / SFTP connection issue using Tamir.SharpSsh

c#sshsftpsharpssh

提问by jinsungy

This is my code to connect and send a file to a remote SFTP server.

这是我连接并将文件发送到远程 SFTP 服务器的代码。

public static void SendDocument(string fileName, string host, string remoteFile, string user, string password)
        {            
            Scp scp = new Scp();

            scp.OnConnecting += new FileTansferEvent(scp_OnConnecting);
            scp.OnStart += new FileTansferEvent(scp_OnProgress);
            scp.OnEnd += new FileTansferEvent(scp_OnEnd);
            scp.OnProgress += new FileTansferEvent(scp_OnProgress);

            try
            {
                scp.To(fileName, host, remoteFile, user, password);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

I can successfully connect, send and receive files using CoreFTP. Thus, the issue is not with the server. When I run the above code, the process seems to stop at the scp.To method. It just hangs indefinitely.

我可以使用 CoreFTP 成功连接、发送和接收文件。因此,问题不在于服务器。当我运行上面的代码时,进程似乎在 scp.To 方法处停止。它只是无限期地挂起。

Anyone know what might my problem be? Maybe it has something to do with adding the key to the a SSH Cache? If so, how would I go about this?

有谁知道我的问题是什么?也许它与将密钥添加到 SSH 缓存有关?如果是这样,我将如何处理?

EDIT:I inspected the packets using wireshark and discovered that my computer is not executing the Diffie-Hellman Key Exchange Init. This must be the issue.

编辑:我使用wireshark检查了数据包,发现我的计算机没有执行Diffie-Hellman Key Exchange Init。应该是这个问题。

EDIT:I ended up using the following code. Note, the StrictHostKeyChecking was turned off to make things easier.

编辑:我最终使用了以下代码。请注意,StrictHostKeyChecking 已关闭以使事情变得更容易。

            JSch jsch = new JSch();
            jsch.setKnownHosts(host);

            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);

            System.Collections.Hashtable hashConfig = new System.Collections.Hashtable();
            hashConfig.Add("StrictHostKeyChecking", "no");
            session.setConfig(hashConfig);

            try
            {
                session.connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp c = (ChannelSftp)channel;

                c.put(fileName, remoteFile);

                c.exit();            
            }
            catch (Exception e)
            {
                throw e;
            }

Thanks.

谢谢。

采纳答案by Bruce Blackshaw

Without looking at your log files it is hard to tell what the issue is.

如果不查看您的日志文件,则很难判断问题是什么。

However keep in mind that SCP is not SFTP - they are completely different protocols that run over SSH. It is possible that your SFTP does not actually support SCP - not all SFTP servers do. CoreFTP may be using SFTP.

但是请记住,SCP 不是 SFTP - 它们是通过 SSH 运行的完全不同的协议。您的 SFTP 可能实际上并不支持 SCP - 并非所有 SFTP 服务器都支持。CoreFTP 可能正在使用 SFTP。

Our commercial package, edtFTPnet/PRO, might also be worth trying, if only as an alternative to try to get a different client working against your server.

我们的商业软件包edtFTPnet/PRO也可能值得尝试,如果只是作为尝试让不同的客户端在您的服务器上工作的替代方法。

回答by Jeganinfo

I use Tamir.SharpSSH - latest version 1.1.1.13

我使用 Tamir.SharpSSH - 最新版本 1.1.1.13

This has a class SFTP. You can use this class directly to do SFTP instead of using JSch, Session class.

这有一个类 SFTP。你可以直接使用这个类来做SFTP,而不用使用JSch、Session类。

Quick Sample here:

快速样品在这里:

127.0.0.1 - Server IP

127.0.0.1 - 服务器 IP

/SFTPFolder1/SFTPFolder2 - Server Location Where I want my files to go

/SFTPFolder1/SFTPFolder2 - 我希望我的文件去的服务器位置

        Sftp sftpClient = new Sftp("127.0.0.1", "myuserName", "MyPassword");

        sftpClient.Connect();

        sftpClient.Put(@"C:\Local\LocalFile.txt", "/SFTPFolder1/SFTPFolder2");

Let me know if you have any issues.

如果您有任何问题,请告诉我。