简体   繁体   中英

get time when a file was copied on sftp using SSH.NET library

I need to get the time when a file was copied on a sftp using SSH.NET library. But the SftpFile class returns only when the file was accessed and modified (also have the option to return the timestamp in UTC). But I need to get the timestamp when the file was copied on the sftp. Here is what I've tried:

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;
}

but the code above crashes with the exception "Specified argument was out of the range of valid values.\\r\\nParameter name: length" at the line ssh.RunCommand(comm) . Is there another way to achieve this using this library?

Regards

I guess this depends a bit on the System that is used at the remote end. If you look at this post: https://unix.stackexchange.com/questions/50177/birth-is-empty-on-ext4

I assume on the remote end there is some kind of Unix, but specifying it would help.

The error you are seeing is probably not coming from the SSH.NET library itself but from the command you are generating. Can you print the comm variable for a run where you get this error? It is probably an issue with quoting the arguments, eg remotepath contains spaces.

I took your example and run it on Mono, it is working fine. As discussed in aboth article, the time of birth of a file might not be exposed to the stat command on your system, it isn't on mine which is a Ubuntu 14.04.3 LTS. If this is the case on your system and you can deposit a script on the remote system, take the get_crtime script from the cited post and trigger it through ssh. It seems on newer systems with ext4fs stat will return the creation date.

Working example for modification time:

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);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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