Skip to content

Instantly share code, notes, and snippets.

@srkirkland
Created January 31, 2022 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srkirkland/1020a680410a9f766e113bcc3359e31c to your computer and use it in GitHub Desktop.
Save srkirkland/1020a680410a9f766e113bcc3359e31c to your computer and use it in GitHub Desktop.
SSH dotnet
Just an example of running an ssh command on a remote server.
PK file needs to come from somewhere -- I think ideally Secret Vault but also base64 encoded secret would be a good choice.
Library used is https://www.nuget.org/packages/SSH.NET
using Microsoft.AspNetCore.Mvc;
using Renci.SshNet;
namespace farm_ssh.Controllers;
[ApiController]
[Route("[controller]")]
public class FarmController : ControllerBase
{
private readonly ILogger<FarmController> _logger;
public FarmController(ILogger<FarmController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetHome")]
public IEnumerable<string> Get()
{
var pkFile = new PrivateKeyFile("./remote-api-auth.pk");
// get list of file in home directory
using (var client = new SshClient("remoteurl.com", "remote-api", pkFile))
{
client.Connect();
var result = client.RunCommand("ls -l");
client.Disconnect();
return result.Result.Split('\n');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment