I have 3 servers (Dediceted Server).

I connect to them using SSH C #.

I have to execute one command on all three servers simultaneously, but they are executed in turn, first go to the first server, execute the command, then the second, also execute the command, and then go to the last server.


using (var client1 = new SshClient("96.102.49.253", "root "Пароль")) { client1.Connect(); client1.RunCommand("команда"); } 

Something like this, only here is 1 server, and I need 3, is it possible to combine them somehow?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • About deleting: StackOverflow doesn't work that way. Your question and its answers should stay here for future readers (of which there will be hundreds or thousands). The whole point is to accumulate a base of ready-made solutions. - Nick Volynkin

1 answer 1

I would do something like this:

 var params = new List<Param>(); params.Add(new Param{IP = "", Login = "", Password = ""}); ... заполняем так или иначе лист params данными, под которыми надо подключаться Parallel.ForEach(params, v => { using (var client1 = new SshClient(v.IP, v.Login, v.Password)) { client1.Connect(); client1.RunCommand("команда"); } }); class Param { public string IP { get; set;} public string Login { get; set;} public string Password { get; set;} } 
  • Invalid "params" element in expression. I need to do so that it was still when I pressed the button. - user199587
  • Everything figured out, thanks :) - user199587