26
2025
02
09:43:29

使用ssh连接远程主机(ssh.net演示)

使用ssh连接远程主机(ssh.net演示)

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




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/9183.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: