Search code examples
c#dockersshfreeradius

Run multiple commands through SSH inside docker container from C#


I need to run multiple commands in docker container through SSH from C#. I am using Renci.sshnet for ssh connection. Here is my code:

  {
      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();
  }

if manually i am doing , these two lines commands working fine for me. but from ssh not working...any idea please???


Solution

  • You can set your complete command in bash parameter -c:

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

    So your command will be like this:

    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 + "\"")