How can I enable/disable ports in Cisco Catalyst 2960 with C#?
Asked Answered
P

3

5

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.

Pomfret answered 7/2, 2012 at 3:34 Comment(0)
C
7

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)

Croaker answered 7/2, 2012 at 5:59 Comment(3)
Thanks Niall, your information is very useful. I am going to study on them now. You are right, I think the 2nd option is probably what I am thinking about. But as you suggested the first one, I am going to try it. :PPomfret
I have tried to enable telnet connection and connected to the switch successfully. So writing a C# program to do port switching would be quite simple. Thanks niall for your option1. Besides, my boss still want to use SNMP in this problem so I am going to try option2 now. So 1.3.6.1.2.1.2.2.1.7 is the target OID and it is a single integer type value. But I have totally 24 ports (FastEthernet 1-24), so should there be 24 values and OIDs?Pomfret
Yes, please look at the the link I included in the answer (You may need to google snmpwalk) > tools.cisco.com/Support/SNMP/do/…Croaker
V
1

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

Vanhomrigh answered 19/9, 2014 at 15:43 Comment(0)
R
0

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 ,

Residual answered 3/3, 2014 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.