Solved

Program Cisco switches by C#

Posted on 2009-05-17
3
2,570 Views
Last Modified: 2013-12-17
Hi
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
        {
            get
            {
                if (_client == null)
                    _client = new TcpClient();
                return _client;
            }
        }
 
        public Cisco(IPEndPoint endPoint)
        {
            Client.Connect(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);
                ia.AsyncWaitHandle.WaitOne();
                read.Add(ASCIIEncoding.ASCII.GetString(buffer.Take((int)((object[])ia.AsyncState)[2]).ToArray()));
            }
            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("172.16.1.147"), 23));
            Console.Write(c.RunCommand("PASSWORD"));
            Console.Write(c.RunCommand("enable"));
            Console.Write(c.RunCommand("ENABLE PASSWORD"));
            Console.Write(c.RunCommand("show run"));
            Console.Write(c.RunCommand("exit"));
            Console.Read();
        }

Open in new window

0
Comment
Question by:TheMehrdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
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 :-|.

0
 
LVL 5

Author Comment

by:TheMehrdad
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.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 24413387
AFAIK there is no other way.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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