Renci.SshNet Demo


本教程使用的是ssh.net這個庫。項目地址:https://github.com/sshnet/SSH.NET

 

使用ssh客戶端連接遠程主機執行命令,並拿到輸出結果:

 

復制代碼
using (var sshClient = new SshClient("host", port,"username", "password"))

{
    sshClient.Connect();
    using (var cmd = sshClient.CreateCommand("ls -l"))
    {
        var res = cmd.Execute();
        Console.Write(res);
    }
}
復制代碼

 

 

使用sftp客戶端上傳文件:

using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
    sftpClient.Connect();
    sftpClient.UploadFile(File.Open(@"D:\index.html", FileMode.Open),"/root/index.html");
}

 

 

下載文件:

復制代碼
using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
    sftpClient.Connect();
    using (var stream = File.Open(@"F:\index.html", FileMode.OpenOrCreate))
    {
        sftpClient.DownloadFile("/root/index.html", stream);
    }
}
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM