簡體   English   中英

使用SSH.NET庫獲取將文件復制到sftp上的時間

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

我需要獲取使用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;
}

但是上面的代碼崩潰,但在ssh.RunCommand(comm)ssh.RunCommand(comm)了“指定的參數超出有效值范圍。\\ r \\ n參數名稱:length”的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);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM