Command not accepting Input Data

May 30, 2011 at 5:41 AM

I am trying to take some input from standard input(stdIn) and perform some operation on command with that.

For eg, I gave "TEST=2" in standard Input, and in command i give "echo $(($TEST+1))". So ideally it should echo "3" as result, but it shows "1".

The code looks like this:

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(tbServer.Text, Convert.ToInt32(tbPort.Text), tbUserName.Text, tbPassword.Password);

using (var client = new SshClient(connectionInfo))
                {
                    client.Connect();
                    var shell = client.CreateShell(input, output1, errorOut);
                    shell.Start();SshCommand cmd = client.RunCommand(command);
                    string _result=cmd.Result;
                    string output = string.Empty;
                    output1.Position = 0;
                    StreamReader reader = new StreamReader(output1);
                    output = reader.ReadToEnd();
                    Thread.Sleep(1000);
                    shell.Stop();
                    sb.AppendLine("Shell Stopped..");
                    client.Disconnect();
                 }

Coordinator
May 30, 2011 at 6:19 AM

Hi,

 

I am on vacation now so will be able to check this issue only later this week but from the code above it looks like you mixing two different things.

If you using the command you dont have to create and run the shell since its irrelevant in command case.

 

One guess why it doesnt work correctly in your case is when you running the command it executes command outside context of the shell therefore any environment variables will not work.

This is my understanding of how things work but I might be wrong since I am not a UNIX/LINUX guru.

Once I am back I could test this myself and let you know what I find.

Thanks,

Oleg

Jun 7, 2011 at 8:39 AM

Hi Oleg,

If you are back from your vacation, can you please look into this issue.

The reason why i am creating and running shell as well as using commnad is that i want to pass input stream too. May be the way i implement is wrong. In that case can you please help me in that. The only requirement i have is that whether i can pass input stream to the command or not. If yes, how?

Thanks,

Vipul

Coordinator
Jun 8, 2011 at 2:17 PM

Vipul,

 

Ok, you cannot provide input stream to the command, the reason is that the command need to be know ahead of time and executed as one, so even if you will use a stream to provide command input, it would have to wait until you done with it and then execute the command.

The way command execution works is, it opens a channel for single command execution without running any shell, so if you execute multiple commands it will not remember anything from previous executions, unless you write something to the disk etc'.

 

What you can do is to chain all commands in one line separated by ; if it can fit your scenario or use Shell only.

 

If I at some point become aware of any other solution I will let you know.

 

Thanks,

Oleg