Thursday, October 26, 2017

Tiny SSH client in C#

There is very nice SSH library for .NET called SSH.NET. You can find it here:
https://github.com/sshnet/SSH.NET, also available as a nuget package, so you can install it in a couple of clicks. Here is a brief client example:

using Renci.SshNet;
using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter IP:");
            var ip = Console.ReadLine();

            Console.WriteLine("Enter username:");
            var username = Console.ReadLine();

            Console.WriteLine("Enter password:");
            var password = Console.ReadLine();

            using (var client = new SshClient(ip, username, password))
            {
                client.Connect();

                while (true)
                {
                    var input = Console.ReadLine();
                    if (input.Contains("exit my console app"))
                        break;

                    Console.WriteLine(client.RunCommand(input).Result);
                }
            }
        }
    }
}

No comments:

Post a Comment