none
Execute Cisco CLI command from C# RRS feed

  • Question

  • Visual Studio 2008 - c#

    I would like to configure my cisco firewall via C# ...

    At the moment I can configure it via telnet ... can I do it from a C# application ?

    Thanks.
    Thursday, May 28, 2009 12:33 PM

Answers

  • I did it this way:

    using System.Net.Sockets;

    ...

    byte[] bytes;
    NetworkStream netStream;
    TcpClient tcpClient;

    ...

    tcpClient = new TcpClient();
    tcpClient.Connect("telnetdevice", 23);
    netStream = tcpClient.GetStream();

    // Please notice that \r\n is executed by telnet as a carriage return, so you can put all telnet commands you want!
    Byte[] sendBytes = Encoding.UTF8.GetBytes("user\r\npassword\r\ntelnet command\r\n");

    netStream.Write(sendBytes, 0, sendBytes.Length);
    bytes = new byte[10000];

    // I tried to use some async methods but all of them fail, so I just wait for some seconds to telnet device to finish parsing commands and returning output
    System.Threading.Thread.Sleep(2000);
    netStream.Read(bytes, 0, 10000);
    // Here it is the telnet device output
    returndata = Encoding.UTF8.GetString(bytes);


    That's it.

    HTH
    • Marked as answer by Fabri_Fabri Thursday, June 4, 2009 8:32 AM
    Thursday, June 4, 2009 8:32 AM

All replies

  • I would go here:
    http://developer.cisco.com

    Read up on the SDKs available, ask questions in their forums, etc. Although it would be possible to create an app that reads and writes to the telnet interface of the device, such a task would be fraught with pitfalls. It may be that there is a programmable interface (and appropriate SDK) to allow you to interact with the device easier.
    Thursday, May 28, 2009 5:32 PM
  • I tried to post in a forum of those (http://developer.cisco.com ) but unfortunately no reply.

    I'll go with telnet way ... can you please point me to right direction to use telnet with c# ?

    Thanks.
    Wednesday, June 3, 2009 7:35 AM
  • First you have to establish a TCP connection to the device. You'll do that with System.Net.Sockets.Socket (http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx). I believe Telnet is port 23.

    The basic idea is simple: Read the response from the device, determine what "state" it's in (akin to which menu you're at), display the appropriate dialog (or maybe tab page, how you create the UI is up to you), then send commands based on what the user has entered into the UI, and repeat.

    The problem is that you may or may not be able to determine the path of every single server response so that you always know where you're at in the device's state. What happens when the device returns an error message? Do you know every possible error message that might be returned? The Telnet interface expects a human on the other end, so it may not always return information that can be easily parsed by a computer program.

    Wednesday, June 3, 2009 12:35 PM
  • I did it this way:

    using System.Net.Sockets;

    ...

    byte[] bytes;
    NetworkStream netStream;
    TcpClient tcpClient;

    ...

    tcpClient = new TcpClient();
    tcpClient.Connect("telnetdevice", 23);
    netStream = tcpClient.GetStream();

    // Please notice that \r\n is executed by telnet as a carriage return, so you can put all telnet commands you want!
    Byte[] sendBytes = Encoding.UTF8.GetBytes("user\r\npassword\r\ntelnet command\r\n");

    netStream.Write(sendBytes, 0, sendBytes.Length);
    bytes = new byte[10000];

    // I tried to use some async methods but all of them fail, so I just wait for some seconds to telnet device to finish parsing commands and returning output
    System.Threading.Thread.Sleep(2000);
    netStream.Read(bytes, 0, 10000);
    // Here it is the telnet device output
    returndata = Encoding.UTF8.GetString(bytes);


    That's it.

    HTH
    • Marked as answer by Fabri_Fabri Thursday, June 4, 2009 8:32 AM
    Thursday, June 4, 2009 8:32 AM