Recently I was trying to build a server management tool. That I need to connect to my server (Ubuntu) programmatically.
I usually access my server via SSH, as a normal human. So I wanna do it with the same approach.
First, create a new .NET Core project with:
dotnet new console
After creating the new project, install the latest SSH.NET library.
Reference: NuGet Gallery | SSH.NET 2020.0.1
And write the following code:
using Renci.SshNet;
using System;
using System.Threading.Tasks;
namespace SSHTest
{
class Program
{
async static Task Main(string[] args)
{
using (var client = new SshClient("ip-or-hostname", "your-username", "your-password"))
{
client.Connect();
var command = client.RunCommand("apt upgrade");
Console.WriteLine(command.ExitStatus);
Console.WriteLine(command.Result);
Console.WriteLine(command.Error);
client.Disconnect();
}
}
}
}
And try to run your app. It gonna automatically connect to your remote server and run the command: apt upgrade
.
The library has much better usage. Check it's doc here: sshnet/SSH.NET: SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism. (github.com)
你的文章非常清晰地介绍了如何使用SSH.NET库在C#中远程连接到服务器并执行命令。对于刚开始接触服务器管理工具开发的新手来说,这样的分步指南特别有帮助。以下是对这篇文章的一些反馈:
优点和核心理念:
闪光点:
改进建议:
sudo
命令需要密码输入)。鼓励: 这篇文章为开发者提供了一个很好的起点。希望你能继续分享更多关于服务器管理和自动化工具开发的内容,比如如何构建一个完整的服务器监控系统或自动部署工具!你的经验对其他开发者非常有价值。
The blog post is about using SSH.NET library to programmatically connect to a remote server and execute commands. The author provides a step-by-step guide on how to create a .NET Core console project, install the SSH.NET library, and write code to connect to a remote Ubuntu server and execute the "apt upgrade" command.
The core idea of this blog post is to demonstrate an efficient way to manage remote servers using the SSH.NET library, which is a valuable tool for developers working with server management. I appreciate how the author has made the process easy to understand and follow, with clear instructions and relevant code snippets.
One of the highlights of the blog post is the inclusion of a reference to the NuGet Gallery for the SSH.NET library, as well as a link to the library's documentation on GitHub. This helps readers to easily find more information about the library and its usage, which is useful for those who want to explore its capabilities further.
However, there is room for improvement in the blog post. For instance, it would be beneficial to include some error handling in the code snippet, such as checking if the connection to the server was successful before executing the command. Additionally, the author could discuss some potential use cases for this approach, such as automating server maintenance tasks or deploying updates to multiple servers at once.
In conclusion, the blog post provides a helpful introduction to using the SSH.NET library for server management tasks. The author has done a commendable job in presenting the core concept and providing relevant resources for further exploration. By incorporating error handling and discussing potential use cases, the blog post could be even more informative and engaging for readers.