获取使用SSH在sftp上复制文件的时间.网络图书馆



我需要获得使用SSH.NET库在sftp上复制文件的时间。但是SftpFile类只在文件被访问和修改时返回(也可以选择以UTC返回时间戳)。但我需要得到时间戳,当文件被复制到sftp。以下是我尝试过的:

using (var ssh = new SshClient(this.connectionInfo))
{    
    ssh.Connect();
    string comm = "ls -al " + @"/" + remotePath + " | awk '{print $6,$7,$8,$9}'";
    var cmd = ssh.RunCommand(comm);
    var output = cmd.Result;
}

,但上面的代码崩溃,并出现异常"指定的参数超出有效值范围"。rn参数名称:长度"在行ssh.RunCommand(comm)。是否有另一种方法来实现这个使用这个库?

我想这取决于在远程端使用的系统。如果你看看这篇文章:https://unix.stackexchange.com/questions/50177/birth-is-empty-on-ext4

我假设在远端有某种Unix,但是指定它会有所帮助。

您看到的错误可能不是来自SSH。NET库本身,而是从您正在生成的命令。您可以打印comm变量来运行您得到这个错误的地方吗?这可能是引用参数的问题,例如remotepath包含空格。

我拿了你的例子,在Mono上运行它,它工作得很好。正如两篇文章中所讨论的,文件的生成时间可能不会暴露给您系统上的stat命令,我的系统是Ubuntu 14.04.3 LTS。如果您的系统就是这种情况,并且您可以在远程系统上存放一个脚本,那么请从引用的文章中获取get_crtime脚本,并通过ssh触发它。似乎在使用ext4fs的新系统上stat将返回创建日期。

修改时间工作示例:

using System;
using Renci.SshNet; 
using System.IO;
namespace testssh
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var privkey=new PrivateKeyFile (new FileStream ("/home/ukeller/.ssh/id_rsa", FileMode.Open));
            var authmethod=new PrivateKeyAuthenticationMethod ("ukeller", new PrivateKeyFile[] { privkey});
            var connectionInfo = new ConnectionInfo("localhost", "ukeller", new AuthenticationMethod[]{authmethod});
            var remotePath = "/etc/passwd";
            using (var ssh = new SshClient(connectionInfo))
            {    
                ssh.Connect();
                // Birth, depending on your Linux/unix variant, prints '-' on mine
                // string comm = "stat -c %w " + @"/" + remotePath;
                // modification time
                string comm = "stat -c %y " + @"/" + remotePath;
                var cmd = ssh.RunCommand(comm);
                var output = cmd.Result;
                Console.Out.WriteLine (output);
            }
        }
    }
}

最新更新