login about faq

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#

asked Jul 25 at 07:08

marhyno's gravatar image

marhyno
32

edited Jul 25 at 07:15


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);
            }
link

answered Jul 28 at 06:23

marhyno's gravatar image

marhyno
32

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.

link

answered Aug 09 at 12:17

chilkat's gravatar image

chilkat ♦♦
12.8k317367485

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×106
×39
×11

Asked: Jul 25 at 07:08

Seen: 321 times

Last updated: Aug 09 at 12:17

powered by OSQA