Hello-
I am using expect to run a command on some Cisco devices. I have found that expect will put all the command output into a string for certain devices and won't for others. For example Expect works with an IOS Router or Catalyst switch. However it doesn't work correctly if one connects to a NXOS(nexus) switch. Basically when connecting to the Nexus device the command output gets lost. It never shows up in the output expect string. I have also not been able to get the information by using read or readline. The latter is true for any Expect connection that I have tried though.
I have two examples of compilable code below that have associated output. Expect1.cs connects to an IOS Router. Expect2.cs connects to a NXOS 3064 switch. If someone thinks I should be able to use read and readline in conjuction with expect please let know how it is supposed to be used. Everything I have tried results in empty strings.
Thanks in Advance.
I am using expect to run a command on some Cisco devices. I have found that expect will put all the command output into a string for certain devices and won't for others. For example Expect works with an IOS Router or Catalyst switch. However it doesn't work correctly if one connects to a NXOS(nexus) switch. Basically when connecting to the Nexus device the command output gets lost. It never shows up in the output expect string. I have also not been able to get the information by using read or readline. The latter is true for any Expect connection that I have tried though.
I have two examples of compilable code below that have associated output. Expect1.cs connects to an IOS Router. Expect2.cs connects to a NXOS 3064 switch. If someone thinks I should be able to use read and readline in conjuction with expect please let know how it is supposed to be used. Everything I have tried results in empty strings.
Thanks in Advance.
//Expect1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Renci.SshNet;
//C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:exe /r:C:\Users\ande\Desktop\CSharp\Renci.SshNet.dll Expect1.cs
namespace ExpectTest
{
class Expect
{
static void Main(string[] args)
{
string prompt = @".+#$";
SshClient shell = new SshClient("IOS Router Device or Catalyst IOS", "user", "pass");
shell.Connect();
if (shell.IsConnected)
{
Console.WriteLine("Connected");
}
var stream = shell.CreateShellStream("dumb",80, 24, 800, 600, 1024);
var a = stream.Expect(new Regex(prompt), new TimeSpan(0, 0, 5));
Console.WriteLine(a);
stream.WriteLine("show ip arp");
var r = stream.Expect(new Regex(prompt), new TimeSpan(0, 0, 5));
Console.WriteLine(r);
}
}
}
/*OUTPUT
C:\Users\ande\Desktop\CSharp>.\Expect1.exe
Connected
NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
Unauthorized access and/or use prohibited. All access and/or use subject to monitoring.
NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
device-name line 645
3945-Router1#
show ip arp
Protocol Address Age (min) Hardware Addr Type Interface
Internet 10.5.224.1 162 0000.0c07.acfa ARPA GigabitEthernet0/1
Internet 10.5.224.2 3 4403.a7a5.2281 ARPA GigabitEthernet0/1
Internet 10.5.224.3 0 0006.f619.e8bc ARPA GigabitEthernet0/1
Internet 10.5.224.116 - c067.af42.5f01 ARPA GigabitEthernet0/1
3945-Router1#
*/
//Expect2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Renci.SshNet;
//C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:exe /r:C:\Users\ande\Desktop\CSharp\Renci.SshNet.dll Expect2.cs
namespace ExpectTest
{
class Expect
{
static void Main(string[] args)
{
//string prompt = @".+#$";
SshClient shell = new SshClient("NXOS Device", "user", "pass");
shell.Connect();
if (shell.IsConnected)
{
Console.WriteLine("Connected");
}
var stream = shell.CreateShellStream("dumb",80, 24, 800, 600, 1024);
//var a = stream.Expect(new Regex(prompt), new TimeSpan(0, 0, 5));
var a = stream.Expect("NX3064_01#", new TimeSpan(0, 0, 5));
Console.WriteLine(a);
stream.WriteLine("show ip arp");
//var r = stream.Expect(new Regex(prompt), new TimeSpan(0, 0, 5));
var r = stream.Expect("NX3064_01#", new TimeSpan(0, 0, 5));
Console.WriteLine(r);
}
}
//If regular expressions are used for expect, both a and r are empty
//If a regular expression for a is used and a name or regex is used for the second, r stays empty.
/*OUTPUT
C:\Users\ande\Desktop\CSharp>.\Expect2.exe
Connected
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2013, Cisco Systems, Inc. All rights reserved.
The copyrights to certain works contained in this software are
owned by other third parties and used and distributed under
license. Certain components of this software are licensed under
the GNU General Public License (GPL) version 2.0 or the GNU
Lesser General Public License (LGPL) Version 2.1. A copy of each
such license is available at
http://www.opensource.org/licenses/gpl-2.0.php and
http://www.opensource.org/licenses/lgpl-2.1.php
NX3064_01#*/
}