Saturday, December 18, 2010

Finding available Ports on a Cisco Switch using .NET and SNMP

Using the SNMP Class mentioned previously in a blog, you can also find out the available MAC Addresses on the switch.  Be sure to reference the assembly in the beginning.



 public void ConnectCiscoSwitch(string arg0, string arg1)
    {
        int commlength, miblength, datastart, datalength;
        string nextmib, value;
        SNMP conn = new SNMP();
        string mib = "1.3.6.1.2.1.17.4.3.1.1";
        int orgmiblength = mib.Length;
        byte[] response = new byte[1024];
        nextmib = mib;


        Label1.ForeColor = Color.Black;
        this.Label1.Style["text-align"] = "left";


        Label1.Text = "<br />Switch <b>" + arg0 +
           "</b> Usage information: <br />";
        Label1.Text +=
            "----------------------------------------------<br />";
        try
        {
            while (true)
            {
                response = conn.get("getnext", arg0, arg1, nextmib);
                commlength = Convert.ToInt16(response[6]);
                miblength = Convert.ToInt16(response[23 + commlength]);
                datalength = Convert.ToInt16(response[25 + commlength + miblength]);
                datastart = 26 + commlength + miblength;
                value = BitConverter.ToString(response, datastart, datalength);
                nextmib = conn.getnextMIB(response);
                if (!(nextmib.Substring(0, orgmiblength) == mib))
                    break;
                Console.WriteLine("{0} = {1}", nextmib, value);
                Label1.Text += nextmib + " = " + value + "<br />";
            }
        }
        catch (Exception ex)
        {
            Label1.ForeColor = Color.Maroon;
       
            Label1.Text = "Sorry! Switch could not be found for <b>" + 
                txtServer.Text + "</b>. " + ex.Message;


        }
    }

No comments:

Post a Comment