SharpSSH 进度和重命名

发布于 2024-09-15 10:47:23 字数 753 浏览 5 评论 0原文

使用该库连接到远程服务器并复制文件。我的流程运行得相当好,但有一些较小的事情我似乎无法解决,因为该库的文档相当薄弱。

我有两个日常工作。一个使用 Tamir.SharpSsh 类,另一个使用 Tamir.SharpSsh.jsch 类。

  1. 使用 Tamir.SharpSsh 类,我可以将文件从本地服务器复制到远程服务器并利用 pogress 事件。我无法做的是确定远程服务器上的特定文件 /Report/data.txt 是否存在于服务器上。如果存在或不存在,我需要采取不同的操作。另外我如何重命名远程服务器上的文件。我尝试过将 SshExec 与“重命名”、“rn”和“mv”命令一起使用,但它似乎不起作用。

  2. 使用 Tamir.SharpSsh.jsch 我可以将文件从本地服务器复制到远程服务器。我还可以重命名远程服务器上的文件。我对这个类不能做的是利用进度事件来跟踪复制进度。另外,我似乎无法找到一种好方法来测试服务器上是否存在特定文件。我想出的方法很粗糙,我能想出的唯一测试方法就是使用

     Dim c As ChannelSftp
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count
    

当一个或多个文件存在时我得到一个计数没有问题。当没有文件时,会抛出异常。

不管怎样,我有日常工作,只是一些我需要帮助的小事情。

蒂亚 AGP

Using the library to connect to a remote server and copy a file. I have the process working fairly well but have some smaller things which I cant seem to resolve as documentation for the library is fairly thin.

I have two routines working. One using the Tamir.SharpSsh class and the other using the Tamir.SharpSsh.jsch class.

  1. Using the Tamir.SharpSsh class I am able to copy the file from the local server to the remote server and tap into the pogress event. What I can't do is determine if a particular file on the remote server say /Report/data.txt exists on the server. I need to take different actions if it exists or if doesn't exist. Also how would I rename a file on the remote server. Ive tried using SshExec with a 'rename', 'rn', and 'mv' command and it doesn't seem to work.

  2. Using the Tamir.SharpSsh.jsch I can copy the file from the local server to the remote server. I can also rename the file on the remote server. What I cant do with this class is to tap into the progress event to keep track of the copy progress. Also I cant seem to find a good way to test to see if a particular file exists on the server. What I have come up with is crude and the only way that I could come up with to test and that is to use

        Dim c As ChannelSftp
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count
    

When one or more file exists I get a count no problem. When there is no file then an exception is thrown.

Anyway, I have the routines working its just some minor things I need help with.

tia
AGP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩物 2024-09-22 10:47:23

您可以使用要检查是否存在的文件的路径调用 Tamir.SharpSsh.Sftp.GetFile 方法(例如 C# 中的示例,抱歉):

private bool FileExists(string filePath)
{
    try
    {
        SftpConnection connection = new SftpConnection(_host, _username, _password);
        connection.Connect(_port);
        connection.Get(filePath, _toDir);
    }
    catch(JSchException)
    {
        return false;
    }
    return true;
}

通过使用这个库 - 就像缺少 GetFileInfo 方法或递归 Gets 和 Puts 一样。但总的来说,它完成了工作。

简单的事实是,Tamir.SharpSsh 无法远程重命名文件 - 它只是没有实现该功能。您可以购买具有更多功能的更好的库,例如:

  • Kellerman Software .NET SFTP 库
  • wodSFTP.NET
  • Rebex SFTP for .NET
  • edtFTPnet/PRO

或者您可以扩展 SharpSsh,因为它是开源的。

You can call the Tamir.SharpSsh.Sftp.GetFile method using the path of the file you want to check exists (example in C#, sorry):

private bool FileExists(string filePath)
{
    try
    {
        SftpConnection connection = new SftpConnection(_host, _username, _password);
        connection.Connect(_port);
        connection.Get(filePath, _toDir);
    }
    catch(JSchException)
    {
        return false;
    }
    return true;
}

I have also noticed a few other issues through my use of this library - like a lack of a GetFileInfo method or recursive Gets and Puts. But overall it get the job done.

The simple fact is, Tamir.SharpSsh can't rename a file remotely - it just does not implement that functionality. You can purchase a better library that has far more features, such as:

  • Kellerman Software .NET SFTP Library
  • wodSFTP.NET
  • Rebex SFTP for .NET
  • edtFTPnet/PRO

or you could extend SharpSsh, since it is open source.

橙味迷妹 2024-09-22 10:47:23

您的问题是由于 SFTP 协议的限制造成的。
- 要检查文件是否存在,请尝试返回该文件的属性;
- 大多数服务器目前不支持文件重命名。

Your issues are because of limitations of SFTP protocol.
- to check file existence, try to return attributes of that file;
- most servers don't support file renaming for now.

巡山小妖精 2024-09-22 10:47:23

是的,我尝试了与 Tamir.SharpSsh.jsch 类似的方法,但对我来说,你必须捕获异常才能检测文件不存在,这似乎很奇怪。这是我发布后所做的:

Private Function FileExistsOnServer(ByVal c As ChannelSftp, ByVal sRemoteFile As String) As Boolean
    Try
        'get a file listing of the file
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count

        'if the count is greater than zero then the file already exists. if its 0 then the file does
        'not exist on the server
        If cnt > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        'if we get an exception then assume the file does not exist on the server
        Return False
    End Try
End Function

yeah I tried something similar with Tamir.SharpSsh.jsch but it seems odd to me that you have to catch the exception to detect non-existence of a file. here is what i did after I posted:

Private Function FileExistsOnServer(ByVal c As ChannelSftp, ByVal sRemoteFile As String) As Boolean
    Try
        'get a file listing of the file
        Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
        Dim cnt As Integer = vct.Count

        'if the count is greater than zero then the file already exists. if its 0 then the file does
        'not exist on the server
        If cnt > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        'if we get an exception then assume the file does not exist on the server
        Return False
    End Try
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文