如何使用C#在Cisco Catalyst 2960中启用/禁用端口? [英] How can I enable/disable ports in Cisco Catalyst 2960 with C#?

查看:92
本文介绍了如何使用C#在Cisco Catalyst 2960中启用/禁用端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我是应届毕业生,刚在香港担任程序员的第一份工作.如主题所述,我需要使用C#来控制Cisco交换机上的端口.

我已经进行了很长时间的搜索和研究,因此我具有有关SNMP和MIB的基本知识.我可以找到一些有关如何管理cisco交换机的文章,但是都没有指定如何启用和禁用端口.此时,我认为我需要配置交换机,以使其启用SNMP服务,然后我应该发送SNMP Set数据包以打开/关闭特定端口.正确吗?

有人对此有一些经验并想与我分享吗?请留下一些建议.如果您以前阅读过一些有用的网站,请将该网址保留在此处,以便我也可以看看.

非常感谢您的关注.

Hi. I am a fresh graduate and have just got my first job as a programmer in Hong Kong. As the topic described, I need to use C# to control the ports on the Cisco switch.

I have search and study for quite a long time so I have the basic knowledge about SNMP and MIB. I can find some articles talking about how to manage a cisco switch but none of them specified how do I enable and disable ports. At this moment I think I need to config the switch so that it enable the SNMP service, then I should send a SNMP Set packet to turn on/off the particular port. Is it correct?

Anyone have some experience on it and like to share with me? Please leave some suggestion. And if you have read some useful websites before, please kindly leave the url here so I can have a look too.

Thank you so much for your attention.

推荐答案

首先,这里是一个很好的文档:

http://oreilly.com/catalog/esnmp/chapter/ch02.html [ ^ ]

这是与路由器通信的代码片段:

Firstly here is a good documentation :

http://oreilly.com/catalog/esnmp/chapter/ch02.html[^]

Here is a code snippet which communicated with a router :

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System.Net;
using System.Net.Sockets;

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

public class CiscoRouter : Form
{

   private TextBox host;
   private TextBox community;
   private ListBox results;
   private Thread monitor;
   private FileStream fs;
   private StreamWriter sw;

   public CiscoRouter()
   {
      Text = "Cisco Router Utilization";
      Size = new Size(400, 380);

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Host:";
      label1.AutoSize = true;
      label1.Location = new Point(10, 30);

      host = new TextBox();
      host.Parent = this;
      host.Size = new Size(170, 2 * Font.Height);
      host.Location = new Point(40, 27);

      Label label2 = new Label();
      label2.Parent = this;
      label2.Text = "Community:";
      label2.AutoSize = true;
      label2.Location = new Point(10, 60);

      community = new TextBox();
      community.Parent = this;
      community.Size = new Size(170, 2 * Font.Height);
      community.Location = new Point(75, 57);

      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(10, 85);
      results.Size = new Size(360, 18 * Font.Height);

      Button start = new Button();
      start.Parent = this;
      start.Text = "Start";
      start.Location = new Point(250, 52);
      start.Size = new Size(5 * Font.Height, 2 * Font.Height);
      start.Click += new EventHandler(ButtonStartOnClick);

      Button stop = new Button();
      stop.Parent = this;
      stop.Text = "Stop";
      stop.Location = new Point(320, 52);
      stop.Size = new Size(5 * Font.Height, 2 * Font.Height);
      stop.Click += new EventHandler(ButtonStopOnClick);
   }

   void ButtonStartOnClick(Object obj, EventArgs ea)
   {
      monitor = new Thread(new ThreadStart(checkRouter));
      monitor.IsBackground = true;
      monitor.Start();
   }

   void ButtonStopOnClick(Object obj, EventArgs ea)
   {
      monitor.Abort();
      sw.Close();
      fs.Close();
   }

   void checkRouter()
   {
      int commlength, miblength, datastart, cpuUtil;
      SNMP conn = new SNMP();
      byte[] response = new byte[1024];
      DateTime time;
      string logFile = "routerlog.txt";
      fs = new FileStream(logFile, FileMode.OpenOrCreate,
            FileAccess.ReadWrite);
      sw = new StreamWriter(fs);

      while (true)
      {
         response = conn.get("get", host.Text,
             community.Text, "1.3.6.1.4.1.9.2.1.58.0");
         if (response[0] == 0xff)
         {
            results.Items.Add("No reponse from host");
            sw.WriteLine("No response from host");
            sw.Flush();
            break;
         }
         commlength = Convert.ToInt16(response[6]);
         miblength = Convert.ToInt16(response[23 + commlength]);
         datastart = 26 + commlength + miblength;

         cpuUtil = Convert.ToInt16(response[datastart]);
         time = DateTime.Now;
         results.Items.Add(time + " CPU Utilization: " + cpuUtil + "%");
         sw.WriteLine("{0} CPU Utilization: {1}%", time, cpuUtil);
         sw.Flush();
         Thread.Sleep(5 * 60000);
      }
   }

   public static void Main()
   {
      Application.Run(new CiscoRouter());
   }
}


class SNMP
{
   public SNMP()
   {

   }

   public byte[] get(string request, string host, string community, string mibstring)
   {
      byte[] packet = new byte[1024];
      byte[] mib = new byte[1024];
      int snmplen;
      int comlen = community.Length;
      string[] mibvals = mibstring.Split('.');
      int miblen = mibvals.Length;
      int cnt = 0, temp, i;
      int orgmiblen = miblen;
      int pos = 0;

      // Convert the string MIB into a byte array of integer values
      // Unfortunately, values over 128 require multiple bytes
      // which also increases the MIB length
      for (i = 0; i < orgmiblen; i++)
      {
         temp = Convert.ToInt16(mibvals[i]);
         if (temp > 127)
         {
            mib[cnt] = Convert.ToByte(128 + (temp / 128));
            mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
            cnt += 2;
            miblen++;
         } else
         {
            mib[cnt] = Convert.ToByte(temp);
            cnt++;
         }
      }
      snmplen = 29 + comlen + miblen - 1;  //Length of entire SNMP packet

      //The SNMP sequence start
      packet[pos++] = 0x30; //Sequence start
      packet[pos++] = Convert.ToByte(snmplen - 2);  //sequence size

      //SNMP version
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP version 1

      //Community name
      packet[pos++] = 0x04; // String type
      packet[pos++] = Convert.ToByte(comlen); //length
      //Convert community name to byte array
      byte[] data = Encoding.ASCII.GetBytes(community);
      for (i = 0; i < data.Length; i++)
      {
         packet[pos++] = data[i];
      }

      //Add GetRequest or GetNextRequest value
      if (request == "get")
         packet[pos++] = 0xA0;
      else
         packet[pos++] = 0xA1;

      packet[pos++] = Convert.ToByte(20 + miblen - 1); //Size of total MIB

      //Request ID
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x04; //length
      packet[pos++] = 0x00; //SNMP request ID
      packet[pos++] = 0x00;
      packet[pos++] = 0x00;
      packet[pos++] = 0x01;

      //Error status
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP error status

      //Error index
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP error index

      //Start of variable bindings
      packet[pos++] = 0x30; //Start of variable bindings sequence

      packet[pos++] = Convert.ToByte(6 + miblen - 1); // Size of variable binding

      packet[pos++] = 0x30; //Start of first variable bindings sequence
      packet[pos++] = Convert.ToByte(6 + miblen - 1 - 2); // size
      packet[pos++] = 0x06; //Object type
      packet[pos++] = Convert.ToByte(miblen - 1); //length

      //Start of MIB
      packet[pos++] = 0x2b;
      //Place MIB array in packet
      for(i = 2; i < miblen; i++)
         packet[pos++] = Convert.ToByte(mib[i]);
      packet[pos++] = 0x05; //Null object value
      packet[pos++] = 0x00; //Null

      //Send packet to destination
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                       ProtocolType.Udp);
      sock.SetSocketOption(SocketOptionLevel.Socket,
                      SocketOptionName.ReceiveTimeout, 5000);
      IPHostEntry ihe = Dns.Resolve(host);
      IPEndPoint iep = new IPEndPoint(ihe.AddressList[0], 161);
      EndPoint ep = (EndPoint)iep;
      sock.SendTo(packet, snmplen, SocketFlags.None, iep);

      //Receive response from packet
      try
      {
         int recv = sock.ReceiveFrom(packet, ref ep);
      } catch (SocketException)
      {
         packet[0] = 0xff;
      }
      return packet;
   }

   public string getnextMIB(byte[] mibin)
   {
      string output = "1.3";
      int commlength = mibin[6];
      int mibstart = 6 + commlength + 17; //find the start of the mib section
      //The MIB length is the length defined in the SNMP packet
     // minus 1 to remove the ending .0, which is not used
      int miblength = mibin[mibstart] - 1;
      mibstart += 2; //skip over the length and 0x2b values
      int mibvalue;

      for(int i = mibstart; i < mibstart + miblength; i++)
      {
         mibvalue = Convert.ToInt16(mibin[i]);
         if (mibvalue > 128)
         {
            mibvalue = (mibvalue/128)*128 + Convert.ToInt16(mibin[i+1]);
            i++;
         }
         output += "." + mibvalue;
      }
      return output;
   }
}


从这里摘录:
http://www.java2s.com/Code/CSharp/Network/CiscoRouter.htm [ ^ ]


使用SNMP某种程度上是特定于设备的,因此您找不到能为您提供诸如以下功能的库:


Extracted from here:
http://www.java2s.com/Code/CSharp/Network/CiscoRouter.htm[^]


Working with SNMP is somehow device specific so you can not find a library which provide you facilities like :

Routers[0].Ports[4].Enabled = false ;



因此,似乎您将需要进行大量的研究和调试,以了解如何执行此操作.

希望对您有所帮助.



So it seems that you are going to have a lot of studying and debugging to find out how to do this.

Hope it helps.


我希望以下链接能给您一些帮助.

http://www.cisco.com/en/US/products/hw/开关/ps708/products_tech_note09186a008015c612.shtml [ ^ ]

您必须找到端口的地址和激活它的命令.

之后,您可以启用或禁用C#中的端口

另请参考Cisco Catalyst 2960的数据表.通常,数据表包含产品的所有此类详细信息.
I hope the following link will give you some idea.

http://www.cisco.com/en/US/products/hw/switches/ps708/products_tech_note09186a008015c612.shtml[^]

You Have to find what are the addresses of ports and the command to activate it.

After That u can enable or disable the ports in C#

Also refer the datasheet of Cisco Catalyst 2960. Usually the datasheet consists all such details of the product.


这篇关于如何使用C#在Cisco Catalyst 2960中启用/禁用端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆