0 votes
by (520 points)
edited

This one is weird. It sends the first command and second command properly and also the 3rd, but the fourth is one behind.

example code:

        // update it on the switch.
        Rebex.Net.Ssh ssh = new Ssh();
        ssh.Connect("...");
        ssh.CheckConnectionState();
        ssh.Timeout = 10 * 1000;

        ssh.Login("...", "...");
        string prompt = "";

        // we use the virtual classes for comm
        VirtualTerminal vt = ssh.StartVirtualTerminal();
        VirtualShell shell = new VirtualShell(vt);

        shell.Terminal.Options.Encoding = System.Text.Encoding.UTF8;
        shell.Prompt = Regex.Escape(@"#");

        shell.SendCommand("term len 0");
        Console.WriteLine(shell.ReceivedData);
        Console.WriteLine(shell.ReadAll());
        Console.WriteLine(shell.ReceivedData);

        shell.Prompt = Regex.Escape(prompt.Substring(prompt.LastIndexOf("\n") + 1) + "#");

        shell.SendCommand("show version");
        Console.WriteLine(shell.ReceivedData);
        Console.WriteLine(shell.ReadAll());
        Console.WriteLine(shell.ReceivedData);

        shell.SendCommand("term len 0");
        Console.WriteLine(shell.ReceivedData);
        Console.WriteLine(shell.ReadAll());
        Console.WriteLine(shell.ReceivedData);

        shell.SendCommand("show version");
        Console.WriteLine(shell.ReceivedData);
        Console.WriteLine(shell.ReadAll());
        Console.WriteLine(shell.ReceivedData);

Results:

 c3560-dc7-hje-mgmt-1
 c3560-dc7-hje-mgmt-1#
 term len 0

 c3560-dc7-hje-mgmt-1
 c3560-dc7-hje-mgmt-1#
 show version

 Cisco IOS Software, C3560E Software (C3560E-UNIVERSALK9-M), Version 15.0(1)SE, R
 ELEASE SOFTWARE (fc1)
 Technical Support: http://www.cisco.com/techsupport
 Copyright (c) 1986-2011 by Cisco Systems, Inc.
 Compiled Wed 20-Jul-11 09:21 by prod_rel_team

 ...

 c3560-dc7-hje-mgmt-1#
 term len 0

 c3560-dc7-hje-mgmt-1
 c3560-dc7-hje-mgmt-1#

The last command should have printed out the version as the previous did... you have to call shell.ReadAll() again... to view the results... so it looks like the commands somehow get behind. this looks like a bug. ideas?

3 Answers

0 votes
by (73.6k points)
edited
 
Best answer

I am sorry, I am a little bit lost in the outputs, because I expected totally different outputs at all (not just in 4th command).

I think the problem arises right at the beginning. You should set the prompt appropriately and read all incoming data at the first.

From your code it seems you don't know the exact prompt, but you know that it ends with '#'. So write your code as follows and give it a try:

// update it on the switch.
Rebex.Net.Ssh ssh = new Ssh();
ssh.Connect("...");
ssh.CheckConnectionState();
ssh.Timeout = 10 * 1000;

ssh.Login("...", "...");

// we use the virtual classes for comm
VirtualTerminal vt = ssh.StartVirtualTerminal();
VirtualShell shell = new VirtualShell(vt);

shell.Terminal.Options.Encoding = System.Text.Encoding.UTF8;
shell.Prompt = Regex.Escape(@"#");

string prompt = shell.ReadAll();
shell.Prompt = Regex.Escape(prompt.Substring(prompt.LastIndexOf("\n") + 1) + "#");

shell.SendCommand("term len 0");
Console.WriteLine("RD: " + shell.ReceivedData);
Console.WriteLine("RA: " + shell.ReadAll());
Console.WriteLine("RD: " + shell.ReceivedData);

shell.SendCommand("show version");
Console.WriteLine("RD: " + shell.ReceivedData);
Console.WriteLine("RA: " + shell.ReadAll());
Console.WriteLine("RD: " + shell.ReceivedData);

shell.SendCommand("term len 0");
Console.WriteLine("RD: " + shell.ReceivedData);
Console.WriteLine("RA: " + shell.ReadAll());
Console.WriteLine("RD: " + shell.ReceivedData);

shell.SendCommand("show version");
Console.WriteLine("RD: " + shell.ReceivedData);
Console.WriteLine("RA: " + shell.ReadAll());
Console.WriteLine("RD: " + shell.ReceivedData);
0 votes
by (520 points)
edited

so, after looking at this some more, with your example, it looks like my problem was i wasn't calling shell.ReadAll() once after instantiating the virtual shell class. Because when you first log in, the shell returns some data, that data was in the queue with a prompt and when i called my first ReadAll after sending term len 0, i got that data, not what was returned after i send my first command.

by (73.6k points)
edited

Exactly, VirtualShell class enables you to send commands without necessity to read their responses. But when you call ReadAll it returns output up to the first prompt (not the last one). It is still open experimental class in development, so we probably change that behaviour somewhat before integrating it directly into component.

0 votes
by (15.2k points)
edited

Hi, we have just release new Scripting API in 2014-R2 release. You can read more of its features on Scripting features page.

...