axel10

本教程使用的是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);
    }
}

 

分类:

技术点:

相关文章:

  • 2021-12-09
  • 2021-06-27
  • 2022-12-23
  • 2022-01-03
  • 2021-08-13
  • 2021-12-20
  • 2021-12-05
  • 2021-06-20
猜你喜欢
  • 2021-09-16
  • 2021-12-04
  • 2021-12-01
  • 2021-06-25
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案