2
0

SshConnectionRestorer.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. namespace Renci.SshNet.IntegrationTests
  2. {
  3. internal class SshConnectionRestorer : IDisposable
  4. {
  5. private SshClient _sshClient;
  6. public SshConnectionRestorer(SshClient sshClient)
  7. {
  8. _sshClient = sshClient;
  9. }
  10. public void RestoreConnections()
  11. {
  12. var command = _sshClient.CreateCommand("sudo sed -i '/DenyUsers sshnet/d' /etc/ssh/sshd_config");
  13. var output = command.Execute();
  14. if (command.ExitStatus != 0)
  15. {
  16. throw new ApplicationException(
  17. $"Unblocking user sshnet failed with exit code {command.ExitStatus}.\r\n{output}\r\n{command.Error}");
  18. }
  19. command = _sshClient.CreateCommand("sudo /usr/sbin/sshd.pam");
  20. output = command.Execute();
  21. if (command.ExitStatus != 0)
  22. {
  23. throw new ApplicationException(
  24. $"Resuming ssh service failed with exit code {command.ExitStatus}.\r\n{output}\r\n{command.Error}");
  25. }
  26. }
  27. public void Dispose()
  28. {
  29. _sshClient?.Dispose();
  30. _sshClient = null;
  31. }
  32. }
  33. }