Created
January 31, 2022 19:03
-
-
Save srkirkland/1020a680410a9f766e113bcc3359e31c to your computer and use it in GitHub Desktop.
SSH dotnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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