运行 来自 C# 的 docker 容器内通过 SSH 的多个命令

Run multiple commands through SSH inside docker container from C#

我需要从 C# 通过 SSH 在 docker 容器中 运行 多个命令。我正在使用 Renci.sshnet 进行 ssh 连接。这是我的代码:

  {
      ssh.Connect();
      var command = ssh.CreateCommand("sudo docker exec - it freeradius bash &&"+" echo User-Name=" + username + ",Framed-IP-Address=" + framedipaddress + "| radclient -x " + nasipaddress + ":1700 disconnect a1rp0c9ptio8");
      strReturn = command.Execute().ToString();
  }

如果我手动执行,这两行命令对我来说工作正常。但是 ssh 无法正常工作...有什么想法吗???

您可以在 bash 参数中设置完整的命令 -c:

$ docker exec -it sad_kilby bash -c "echo 123 && echo $? && ls / | grep etc"
123
0
etc

所以你的命令将是这样的:

string subcommand = "echo User-Name=" + username + ",Framed-IP-Address=" + framedipaddress + "| radclient -x " + nasipaddress + ":1700 disconnect a1rp0c9ptio8"
var command = ssh.CreateCommand("sudo docker exec -it freeradius bash -c \"" + subcommand + "\"")