Program Cisco switches by C#

Posted on 2009-05-17
Last Modified: 2013-12-17
I want to use System.Net.Sockets.TcpClient class in .Net framework to configure a Cisco switch or router. Does anyone has any samples or how to?
I already have written the below code. When I run it, I don't get what I expect.
public class Cisco


        private TcpClient _client;

        //private event EventHandler<

        private TcpClient Client




                if (_client == null)

                    _client = new TcpClient();

                return _client;



        public Cisco(IPEndPoint endPoint)



            var s = Read();


        public string RunCommand(string command)


            if (!command.EndsWith("\r\n"))

                command = string.Format("{0}\r\n", command);

            var buffer = ASCIIEncoding.ASCII.GetBytes(command);

            Client.GetStream().Write(buffer, 0, buffer.Length);

            var s = Read();

            return s;


        private string Read()


            var buffer = new Byte[Client.ReceiveBufferSize];

            var read = new List<string>();

            while (Client.GetStream().DataAvailable)


                var state = new object[] { Client.GetStream(), buffer, null };

                var ia = Client.GetStream().BeginRead(buffer, 0, buffer.Length, new AsyncCallback(Read), state);




            return string.Concat(read.ToArray());


        private void Read(IAsyncResult ar)


            var state = ar.AsyncState as object[];

            var stream = state[0] as NetworkStream;

            var buffer = state[1] as byte[];

            var br = stream.EndRead(ar);

            state[2] = br;



     static void Main(string[] args)


             Cisco c = new Cisco(new IPEndPoint(IPAddress.Parse(""), 23));



            Console.Write(c.RunCommand("ENABLE PASSWORD"));

            Console.Write(c.RunCommand("show run"));




Open in new window

Question by:TheMehrdad
  • 2
LVL 68

Accepted Solution

Qlemo earned 250 total points
ID: 24406886
Telnet expects some negotiation of options like emulation, echo aso. If you like, you can have a look at the netcat source, it is a tool for streaming stuff thru a telnet connection, but be warned, it is complex and the source comments are rude :-|.


Author Comment

ID: 24412267
Dear Qlemo,
Thanks for your comment. Is there any other way to configure a Cisco Switch/Router programatically? If Telnet is the only solution, so I will deal with this netcat.
LVL 68

Expert Comment

ID: 24413387
AFAIK there is no other way.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now