从C#在docker容器内通过SSH运行多个命令

问题描述 投票:0回答:1

我需要通过SSH从C#在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不工作......任何想法请???

c# docker ssh freeradius
1个回答
0
投票

您可以在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 + "\"")
© www.soinside.com 2019 - 2024. All rights reserved.