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)
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.