2
0

SshConnectionDisruptor.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. namespace Renci.SshNet.IntegrationTests
  2. {
  3. internal class SshConnectionDisruptor
  4. {
  5. private readonly IConnectionInfoFactory _connectionInfoFactory;
  6. public SshConnectionDisruptor(IConnectionInfoFactory connectionInfoFactory)
  7. {
  8. _connectionInfoFactory = connectionInfoFactory;
  9. }
  10. public SshConnectionRestorer BreakConnections()
  11. {
  12. var client = new SshClient(_connectionInfoFactory.Create());
  13. client.Connect();
  14. PauseSshd(client);
  15. return new SshConnectionRestorer(client);
  16. }
  17. private static void PauseSshd(SshClient client)
  18. {
  19. var command = client.CreateCommand("sudo echo 'DenyUsers sshnet' >> /etc/ssh/sshd_config");
  20. var output = command.Execute();
  21. if (command.ExitStatus != 0)
  22. {
  23. throw new ApplicationException(
  24. $"Blocking user sshnet failed with exit code {command.ExitStatus}.\r\n{output}\r\n{command.Error}");
  25. }
  26. command = client.CreateCommand("sudo pkill -9 -U sshnet -f sshd.pam");
  27. output = command.Execute();
  28. if (command.ExitStatus != 0)
  29. {
  30. throw new ApplicationException(
  31. $"Killing sshd.pam service failed with exit code {command.ExitStatus}.\r\n{output}\r\n{command.Error}");
  32. }
  33. }
  34. }
  35. }