Archived Forum Post

Index of archived forum posts

Question:

Chilkat SSH commands shows in output

Aug 09 '17 at 12:17

Hello, I would like to get rid of the commands I am executing on device. I have Windows user form and when I am sending commands to the switch in the output except the results or action which the application did on the device I also see commands / config. I want to get rid of them and show only result of these commands.

            /*THIS IS THE SET OF COMMANDS I WANT TO RUN */
            ssh2.ChannelSendString(channelNum, command, "ansi");

            if (success != true)
            {
                if (x < 2)
                {
                    output_box.Invoke((MethodInvoker)delegate
                    {
                        // Running on the UI thread
                        output_box.Text += "\r\n Execution of commands failed \r\n Running again ...\r\n";
                        tofile += "\r\n Session failed \r\n Running again ...\r\n ";
                    });
                    x++;
                    i--;
                    continue;
                }
            }

            ssh2.ChannelSendEof(channelNum);
            var cmdOutput = ssh2.ChannelReadAndPoll(channelNum, 2000);
            var result = ssh2.GetReceivedText(channelNum, "ansi");
            tofile += result;

            output_box.Invoke((MethodInvoker)delegate {
                // Running on the UI thread
                output_box.Text += result;
            });

So what I see in the output_box is this -> how do I get rid of the first four lines starting with ,,conf t" and ending with ,,end"

############### SWITCH ##################### - 1 of 2

conf t
vlan 20
name 20
end

SWITCH# conf t

Enter configuration commands, one per line. End with CNTL/Z.

SWITCH(config)# vlan 20

SWITCH(config-vlan)# name 20

SWITCH(config-vlan)# end

SWITCH#


Answer

Okay guys, I managed to filter all unnecessary things with simple C# StringReader function, take a look below. Because I am returning tuple I took first Item and filtered lines. Job done.

            using (StringReader reader = new StringReader(result.Item1))
            {
                string line = string.Empty;
                do
                {
                    line = reader.ReadLine();
                    if (line != null)
                    {
                        if (line == "vlan " + vlan_id)
                        {
                            vlan_exists = 1;
                        }
                        if (line.Contains("  name "))
                        {
                            has_name = 1;
                            vlan_name = line;
                        }
                    }

                } while (line != null);
            }

Answer

There are two ways to run commands on a remote system via SSH:

1) In a remote shell session. This is how you're currently doing it. You start shell, the .bashrc (or whatever) is automatically run in the remote session and you receive the initial command prompt. The stdout of the shell is passed back to your program as channel data, and this includes the next shell prompt, etc. The text you send to the remote shell session is the stdin to the shell. Effectively, each character sent is like someone typed the char at a keyboard in the shell session.

2) The other way or running a remote shell is via the SendReqExec method. This is not a shell session. It is the equivalent of the rexec command in Unix. You can only send a single command this way, but with the proper syntax a single command can effectively be a composition of several commands with the use of semicolons, etc. Using the SendReqExec command might simplify your coding because you're not going to get the shell session related output. You'll just get the output of your command.