繁体   English   中英

SFTP SSH.NET DLL而不是SharpSSH

[英]SFTP SSH.NET DLL instead of SharpSSH

我正在寻找用于.net的免费DLL以处理SFTP连接。

我发现了这个项目SharpSSH ,但是缺少文档。

我花了很多时间弄清楚dll是如何工作的。 我创建了一个测试项目,并开始测试不同的功能。 某些功能正在起作用,例如删除文件。

我对putfile()函数和getfile()有问题。

这是一个例子:

Dim ssh As SFTPUtil
ssh = New SFTPUtil("MY SERVER", "MY USER", "MY PW")
ssh.GetFile("/home/sftptest/test111.xml", "C:\\text.xml")

请注意,getfile()参数为:

Public Sub GetFile(remotePath As String, localPath As String)

我进入了函数,但是没有获得传递这些参数的正确方法。

我真的不知道我应该使用斜杠(/)还是反斜杠()。 我知道Linux使用(/)

例如,我注意到“ C:\\”已转换为“ C:\\\\”。

只需提及SFTP在Linux机器上即可。

谢谢。

这是我应该做的(vb.net代码)建立与连接这个库(SSHnet)没有用SharpSHH:

Private Sub ButtonAction_Click(sender As Object, e As System.EventArgs) Handles ButtonAction.Click

    Dim PasswordConnection = New PasswordAuthenticationMethod("test", "test")
    Dim KeyboardInteractive = New KeyboardInteractiveAuthenticationMethod("test")
    Dim ConnectionInfo = New ConnectionInfo("192.168.1.1", 22, "test", PasswordConnection, KeyboardInteractive)

    AddHandler KeyboardInteractive.AuthenticationPrompt, _
    Sub(_sender As Object, _e As Renci.SshNet.Common.AuthenticationPromptEventArgs)
        For Each prompt In _e.Prompts
            Debug.Print(prompt.Request)
            If Not prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) = -1 Then
                prompt.Response = "test"
            End If
        Next
    End Sub

    sftp = New SftpClient(ConnectionInfo)
    sftp.Connect()

    sftp.disconnect()
End Sub

该库充满了错误。 我在11/2012下载了版本,即使使用断开连接之类的简单功能也遇到了很大的问题。 应用程序将冻结,您必须重新启动它。

我在理解您到底想要什么时遇到了一些问题,因此这里是SSH.NET软件包中的示例代码。 (不是SharpSSH)

string IP = "192.168.1.1", User = "Testuser", Pass = "123";
SftpClient sftp;

private void UploadFileSFTP()
    {
        sftp = new SftpClient(IP, User, Pass);
        sftp.Connect();
        Uploader();
        Downloader();
        sftp.Disconnect();
    }

string FilePath="C:\\folder\\", Filename = "Filename.extention", 
       DeliveryPath = "/tmp/";

private void Uploader()
    {
        using (var file = File.OpenRead(FilePath + Filename))
        {
            sftp.UploadFile(file, DeliveryPath + Filename);
        }
    }

//there is possibly a simpler way to download but this is how i did it.
string FromPath = "/tmp/testfile.txt", StoragePath = ""; 
private void Downloader()
    {
        if (File.Exists(StoragePath))
            File.Delete(StoragePath);
        if (!Directory.GetDirectories(Path.GetTempPath()).Contains("WorkFiles"))
        {
            Directory.CreateDirectory(Path.GetTempPath() + "WorkFiles");
        }

        StoragePath = Path.GetTempPath() + "WorkFiles\\testFile.txt";

        Int64 iSize = sftp.ReadAllBytes(FromPath).Length;
        Int64 iRunningByteTotal = 0;
        using (Stream streamRemote = sftp.OpenRead(FromPath))
        {
            using (Stream streamLocal = new FileStream(StoragePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[iSize];
                while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    streamLocal.Write(byteBuffer, 0, iByteSize);
                    iRunningByteTotal += iByteSize;
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM