Transient Error in Cloud

Today in this article, we will see how to use Reboot/Restart Linux Server using SSH – C#.NET with examples.

We will see the execution of the SSH command using both synchronous and asynchronous methods.

This technique is handy when you need to reboot your server programmatically without using any tool.

Using C# code with simple 2-3 lines of code, you can create a utility and run it as CLI or schedule it to run as per your need.

Prerequisites :

  • Server details including hostname or IP address,

  • Root user name (Admin privilege)

  • Password to connect using SSH

We will explore this option using the SSH.NET library for .NET/.NET Core.

Execution of the SSH command using the SSH.NET library is pretty simple.

Below is the list of high-level features supported by the SSH library (but not limited to),

  • Provide SFTP functionality for both synchronous and asynchronous operations.

  • Provides SCP functionality.

  • Execution of SSH command using both synchronous and asynchronous methods.

  • Provide a status report for upload and download SFTP operations to allow accurate progress bar implementation.

  • Supports public-key, password, and keyboard-interactive authentication methods.

Execution of the SSH command using the SSH.NET library is pretty simple.

Getting Started

Create any .NET Core application like a console application,

Add SSH.NET Nuget package as below,

c# ssh.net examples, c# ssh tunnel, c# ssh.net shell example, c# best ssh library, ssh.net documentation

Package Manager Console,

Install-Package SSH.NET -Version 2020.0.2

Note: Please use the latest version.

SshClient.Connect ()- Connect Remote Server via SSH.NET

SSH.NET provides SFTPClient class that lets you connect to the Linux/Windows server securely and access the various operation on the server including Server reboot.

Below is the sample code to connect the Server using SSH.NET

using (var client = new SshClient("Server/IP", "UserName", "Password"))
            {
                client.Connect();
             }

Please use a try-catch block to catch or troubleshoot for any exceptions. Few common connection issues as below.

SSH exception scenarios

  • System.InvalidOperationException– If the client is already connected

  • System.Net.Sockets.SocketException– if Socket connection to the SSH server or proxy server could not be established.

  • Renci.SshNet.Common.SshAuthenticationException– if SSH session could not be established.

  • Renci.SshNet.Common.ProxyException– Failed to establish proxy connection

Resolution:

  • To resolve any connection-related issues please make sure your server IP is accessible from the application host server.

  • You may need to validate if the firewall is open for the given IP and port.

  • You may also check to IP whitelist the respective IPs from their host.

You may need to adjust the command if your remote system requires a different command to initiate a restart.

Reboot the Linux system using C# Code

Please use the below command to Reboot the Linux system using C# Code,

using (var client = new SshClient("Server/IP", "UserName", "Password"))
            {

                client.Connect();

                client.RunCommand("sudo reboot");
 
                Console.WriteLine("Restart in progress :" + result.Result);

                client.Disconnect();

            }

I also tried the below commands which found to be working fine,

 
var result = client.RunCommand(@"/etc/init.d/httpd restart");

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *