Command with input data

May 25, 2011 at 6:47 AM

Hi, I am new to this forum... so please bear with me.. :)

I am not able to understand how can i use Input data with command, using this API??

Right now.. i am using the API like this...

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(tbServer.Text, tbUserName.Text, tbPassword.Password);
            StringBuilder sb = new StringBuilder();
            try
            {
                using (var client = new SshClient(connectionInfo))
                {
                    client.Connect();
                    sb.AppendLine("Connected to server..");
                    var shell = client.CreateShell(input, output1, errorOut);
                    sb.AppendLine("Shell Created..");
                    shell.Stopped += delegate(object sender, EventArgs e)
                    {
                        sb.AppendLine("Disonnected from server..");
                    };
                    Stream errorStream = null;
                    shell.ErrorOccurred += delegate(object sender, ErrorEventArgs e)
                    {
                        sb.AppendLine("Error occured in Shell..");
                        sb.AppendLine();
                        Exception ex = e.GetException();
                        sb.AppendFormat("Shell Error is {0}", ex.ToString());
                        errorStream = ObjectToStream(ex);
                    };
                    if (errorStream != null)
                    {
                        errorOut = CopyStream(errorStream); //Copying the stream into another Stream variable
                    }
                    shell.Start();
                    sb.AppendLine("Shell Started..");
                    SshCommand cmd = client.RunCommand(command);
                    sb.AppendLine("Command Executed..");
                    result = cmd.ExitStatus;
                    if (cmd.ExitStatus != 0)
                    {
                        MemoryStream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(cmd.Error));
                        errorOut = CopyStream(s);//Copying the stream into another Stream variable
                        errorOut.Position = 0;
                    }
                    else
                    {
                      tbResult.Text = cmd.Result;
                    }

                    string output = string.Empty;
                    output1.Position = 0;
                    StreamReader reader = new StreamReader(output1);
                    output = reader.ReadToEnd();
                    output1.Position = 0;
                    sb.AppendLine();
                    sb.AppendFormat("Output is {0}", output);

                    

                    sb.AppendLine();
                    sb.AppendFormat("Result is {0}", result);
                    Thread.Sleep(1000);
                    shell.Stop();
                    sb.AppendLine("Shell Stopped..");
                    client.Disconnect();
                    sb.AppendLine("Client Disconnected..");
 
 
But suppose, when i use "echo" command with input data as "1234", the output stream after converting it into string we get is something like this:
"Last login: Wed May 25 12:00:52 2011 from 10.117.3.41
1234[test@newstar ~]$ 1234"
 
and result i am getting is BLANK actually.
 
Please tell me where i am going wrong actually.
Thanks
Coordinator
May 25, 2011 at 11:56 AM

Hi,

 

Are you trying to execute single command and get results back or to use shell functionality for something?

If this only a single command  I would look into SshCommand class and RunCommand methods.

Hope it helps,

 

Thanks,

Oleg

May 25, 2011 at 11:58 AM

Hmm.. see the requirement is something like this..

I need to pass Indut data which would do some processing on the command that has been passed.

In that case, how should i proceed?

Let me know if my query is not clear.

May 27, 2011 at 12:13 PM

Please ignore the above post by me.

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