如何使用 C# 启用/禁用 Cisco Catalyst 2960 中的端口?

发布于 2025-01-02 21:56:02 字数 294 浏览 0 评论 0原文

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

我经过很长时间的搜索和学习,对SNMP和MIB有了基本的了解。我可以找到一些讨论如何管理思科交换机的文章,但没有一篇文章指定如何启用和禁用端口。此时我想我需要配置交换机以启用 SNMP 服务,然后我应该发送 SNMP Set 数据包来打开/关闭特定端口。正确吗?

有人有这方面的经验并愿意与我分享吗?请留下一些建议。如果您以前读过一些有用的网站,请在这里留下网址,以便我也可以看看。

非常感谢您的关注。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月牙弯弯 2025-01-09 21:56:02

我知道三种方法(我最喜欢第一种):

选项 1:
处理此类问题的常见方法是自动 telnet(或自动 ssh)来发送适当的命令。 IOS 在很大程度上是相当标准化的,并且以这种方式工作得很好 -
TCL 的 Expect 包非常适合这种事情...我想有一个 C# 实现。

如果您已经熟悉 IOS 语法,则此方法效果最佳。

选项 2:
这可能就是您想要的:
IF-MIB::ifAdminStatus 可通过 SNMP 写入
(以下是使用 NET-SNMP 的示例):

接口 UP:
snmpset -v1 -c 社区主机名 IF-MIB::ifAdminStatus.interface i 1

接口关闭:
snmpset -v1 -c 社区主机名 IF-MIB::ifAdminStatus.interface i 2

(其中“interface”是代表接口的数字,该值在同一 MIB 中可用 - 执行 1.3.6.1.2.1.2.2.1.2 查找接口值和描述。)

(同样可能有一个 C# 实现您可以利用 snmpset...以及
您必须事先在设备上配置 SNMP 社区。)

选项 3:
可以使用 SNMP 触发上传或通过 TFTP 服务器下载配置。
有时按以下顺序使用:
1.下载运行配置
2. 更改配置
3.上传配置
4. 将上传的配置复制到正在运行的配置(覆盖设备上活动的配置)

I know three methods (I am most fond of the first):

Option 1:
A common way to approach something like this is automated telnet (or automated ssh) to send the appropriate commands. IOS is pretty standardized for the most part and works quite well this way-
TCL's Expect package is perfect for this kind of thing... I imagine there is a C# implementation.

This works best if you are already familiar with IOS syntax.

Option 2:
This is probably what you want:
IF-MIB::ifAdminStatus is writable via SNMP
(Here is an example using NET-SNMP):

Interface UP:
snmpset -v1 -c community hostname IF-MIB::ifAdminStatus.interface i 1

Interface DOWN:
snmpset -v1 -c community hostname IF-MIB::ifAdminStatus.interface i 2

(Where 'interface' is a digit that represents a interface, the value is available in the same MIB- do a walk on 1.3.6.1.2.1.2.2.1.2 to find the interface values and descriptions.)

(Again there will likely be a C# implementation of snmpset you can leverage... and
you must configure the SNMP communities on the device beforehand.)

Option 3:
It is possible to use SNMP to trigger an upload or download of config via a TFTP server.
This is sometimes used in this sequence:
1. download running config
2. alter that config
3. upload config
4. copy the uploaded config to the running config (overwrites what's active on the device)

几度春秋 2025-01-09 21:56:02

我发现几个 POE 交换机(cisco 和 zyxel)用于操作 POE 状态的 snmp 字符串是

SNMPv2-SMI::mib-2.105.1.1.1.3.1.x
x 是所需的端口(对于 8 端口交换机 1~8 对于 24 端口交换机 1~24),

提到的 IF-MIB::ifAdminStatus.interface 仅用于启用/禁用端口 - 但主要目标是重置或降低夜间/周末的电力成本
在具有数十/数百个接入点/VoIP 电话的环境中,这可以节省大量能源,

停止端口上 POE 的命令是

snmpset -v 2c -c setcomunity host SNMPv2-SMI::mib-2.105.1.1.1.3.1 .xi 2

在端口上启动 POE 的命令是

snmpset -v 2c -c setcomunity host SNMPv2-SMI::mib-2.105.1.1.1.3.1.xi 1

如果您使用版本 3,您还需要身份验证

I have found for several POE switches ( cisco and zyxel) that the snmp string to manipulate POE state is

SNMPv2-SMI::mib-2.105.1.1.1.3.1.x
with x being the desired port ( for a 8 port switch 1~8 for 24 port switch 1~24 )

the mentioned IF-MIB::ifAdminStatus.interface act only for the enable/disable port - but the main goal is to reset or reduce power cost for over night /weekend
in an environment with tens/hundreds of Access Points / VoIP phones this can save a lot of energy

the command to stop POE on port is

snmpset -v 2c -c setcomunity host SNMPv2-SMI::mib-2.105.1.1.1.3.1.x i 2

the command to start POE on port is

snmpset -v 2c -c setcomunity host SNMPv2-SMI::mib-2.105.1.1.1.3.1.x i 1

if you use version 3 you'll need authentication also

各自安好 2025-01-09 21:56:02

您可以通过这种方式使用 telnet 程序 google“MinimalisticTelnet”并使用示例来理解此解决方案

另一种方法是使用 SNMP 协议,例如以下示例:
注意:为了使用 snmp,我需要更改并自定义 SnmpSharpNet 库,并在我的程序中调用它

using SnmpSharpNet;

然后编写此代码以禁用从 1 到 6 的端口:

Console.WriteLine("Ports Disabler ");
            UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.1.200"));
            Pdu pdu = new Pdu(PduType.Set);
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.1"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.2"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.3"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.4"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.5"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.6"), new Integer32(2));
            AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("2645"));
            SnmpV2Packet response;
            try
            {
                // Send request and wait for response
                response = target.Request(pdu, aparam) as SnmpV2Packet;
            }
            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Console.WriteLine(String.Format("Request failed with exception: {0}", ex.Message));
                target.Close();
                return;
            }
            // Make sure we received a response
            if (response == null)
            {
                Console.WriteLine("Error in sending SNMP request.");
            }
            else
            {
                // Check if we received an SNMP error from the agent
                if (response.Pdu.ErrorStatus != 0)
                {
                    Console.WriteLine(String.Format("SNMP agent returned ErrorStatus {0} on index {1}",
                      response.Pdu.ErrorStatus, response.Pdu.ErrorIndex) + response.ToString());
                }
                else
                {
                    // Everything is ok. Agent will return the new value for the OID we changed
                    Console.WriteLine(String.Format("Agent response {0}: {1}",
                      response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
                }
            }

我希望这个答案有用并且可以帮助您,

You can use telnet program for this way google "MinimalisticTelnet" and use example for understand this solution

Another way is use to SNMP Protocol such as this example :
Notice : for work with snmp i get change and customize SnmpSharpNet library , and call it in my program

using SnmpSharpNet;

Then write this code for disable ports from 1 to 6 :

Console.WriteLine("Ports Disabler ");
            UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.1.200"));
            Pdu pdu = new Pdu(PduType.Set);
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.1"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.2"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.3"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.4"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.5"), new Integer32(2));
            pdu.VbList.Add(new Oid("1.3.6.1.2.1.2.2.1.7.6"), new Integer32(2));
            AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("2645"));
            SnmpV2Packet response;
            try
            {
                // Send request and wait for response
                response = target.Request(pdu, aparam) as SnmpV2Packet;
            }
            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Console.WriteLine(String.Format("Request failed with exception: {0}", ex.Message));
                target.Close();
                return;
            }
            // Make sure we received a response
            if (response == null)
            {
                Console.WriteLine("Error in sending SNMP request.");
            }
            else
            {
                // Check if we received an SNMP error from the agent
                if (response.Pdu.ErrorStatus != 0)
                {
                    Console.WriteLine(String.Format("SNMP agent returned ErrorStatus {0} on index {1}",
                      response.Pdu.ErrorStatus, response.Pdu.ErrorIndex) + response.ToString());
                }
                else
                {
                    // Everything is ok. Agent will return the new value for the OID we changed
                    Console.WriteLine(String.Format("Agent response {0}: {1}",
                      response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
                }
            }

I hope this answer be useful and can help you ,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文