DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • JGit Library Examples in Java
  • How to Create a 1-to-1 Messaging App Using Ejabberd and Erlang?
  • Evolving Domain-Specific Languages
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation

Trending

  • Choosing the Appropriate AWS Load Balancer: ALB vs. NLB
  • DevSecOps: Integrating Security Into Your DevOps Workflow
  • AWS Amplify: A Comprehensive Guide
  • Implementing Stronger RBAC and Multitenancy in Kubernetes Using Istio
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Using SSH.NET

Using SSH.NET

Mike Hadlow user avatar by
Mike Hadlow
·
Jun. 09, 13 · Interview
Like (0)
Save
Tweet
Share
29.91K Views

Join the DZone community and get the full member experience.

Join For Free

I’ve recently had the need to automate configuration of Nginx on an Ubuntu server. Of course, in UNIX land we like to use SSH (Secure Shell) to log into our servers and manage them remotely. Wouldn’t it be nice, I thought, if there was a managed SSH library somewhere so that I could automate logging onto my Ubuntu server, run various commands and transfer files. A short Google turned up SSH.NET by the somewhat mysterious Olegkap (at least I couldn’t find out anything else about them) which turned out to be just what I wanted. Here’s the blurb on the CodePlex site:

“This project was inspired by Sharp.SSH library which was ported from java and it seems like was not supported for quite some time. This library is complete rewrite using .NET 4.0, without any third party dependencies and to utilize the parallelism as much as possible to allow best performance I can get.”

It does exactly what it says on the tin. It’s on NuGet, so you can grab it with:

PM> Install-Package SSH.NET

Here’s how you run a remote command. First you need to build a ConnectionInfo object:

public ConnectionInfo CreateConnectionInfo()
{
    const string privateKeyFilePath = @"C:\some\private\key.pem";    
    ConnectionInfo connectionInfo;
    using (var stream = new FileStream(privateKeyFilePath, FileMode.Open, FileAccess.Read))
    {
        var privateKeyFile = new PrivateKeyFile(stream);
        AuthenticationMethod authenticationMethod =
            new PrivateKeyAuthenticationMethod("ubuntu", privateKeyFile);

        connectionInfo = new ConnectionInfo(
            "my.server.com",
            "ubuntu",
            authenticationMethod);
    }

    return connectionInfo;
}

Then you simply create an SshClient instance and run commands:

public void Connect()
{
    using (var ssh = new SshClient(CreateConnectionInfo()))
    {
        ssh.Connect();
        var command = ssh.CreateCommand("uptime");
        var result = command.Execute();
        Console.Out.WriteLine(result);
        ssh.Disconnect();
    }
}

Here I’m running the ‘uptime’ command which output this when I ran it just now:

14:37:46 up 22 days,  3:59,  0 users,  load average: 0.08, 0.03, 0.05

To transfer a file, just use the ScpClient:

public void GetConfigurationFiles()
{
    using (var scp = new ScpClient(CreateNginxServerConnectionInfo()))
    {
        scp.Connect();

        scp.Download("/etc/nginx/", new DirectoryInfo(@"D:\Temp\ScpDownloadTest"));

        scp.Disconnect();
    }
}

Which grabs all my Nginx configuration and transfers it to a directory tree on my windows machine.

All in all a very nice little library that’s been working well for me so far. Give it a try if you need to interact with a UNIX-like machine from .NET code.

Command (computing) Library Transfer (computing) ubuntu Machine .NET Grab (software) Dependency

Published at DZone with permission of Mike Hadlow, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JGit Library Examples in Java
  • How to Create a 1-to-1 Messaging App Using Ejabberd and Erlang?
  • Evolving Domain-Specific Languages
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: