Wednesday, May 29, 2013

SSH.NET

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.

3 comments:

  1. If you just want to run a command on a remote machine then just use the ssh command:

    eg:

    ssh ubuntu@my.server.com uptime

    ReplyDelete
  2. Hi Mike, I've had pain trying to execute shell scripts on linux boxes from a windows (TeamCity) process. Would love to hear if you've found this setup reliable. I had intermittent failures and found it very hard to (impossible) to diagnose.

    I'm very grateful for ssh.net, it just irks me that ssh isn't native to windows with powershell cmdlets. More and more organisations are taking the right tool for the job approach when building systems and with heterogeneous environments becoming common place I wonder why these tasks can be so troublesome.

    At least there are options like ssh.net, putty and samba. Thank heavens for small mercies...

    ReplyDelete
  3. Hey Mike, just wanted to say thanks for writing this piece. A great help in automating my remote tasks

    ReplyDelete

Note: only a member of this blog may post a comment.