⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn技术中心 snmp上手攻略.mht

📁 SNMP上手攻略
💻 MHT
📖 第 1 页 / 共 5 页
字号:
       =20
        if ((session =3D SnmpMgrOpen(agent, community, timeout, =
retries)) =3D=3D NULL)
        {
            printf("error on SnmpMgrOpen %d\n", GetLastError());
           =20
            return 1;
        }
       =20
    } // end if(TRAP)
   =20
   =20
    // Determine and perform the requested operation.
   =20
    if      (operation =3D=3D GET || operation =3D=3D GETNEXT)
    {
        // Get and GetNext are relatively simple operations to perform.
        // Simply initiate the request and process the result and/or
        // possible error conditions.
       =20
       =20
        if (operation =3D=3D GET)
            requestType =3D ASN_RFC1157_GETREQUEST;
        else
            requestType =3D ASN_RFC1157_GETNEXTREQUEST;
       =20
       =20
        // Request that the API carry out the desired operation.
       =20
        if (!SnmpMgrRequest(session, requestType, &variableBindings,
            &errorStatus, &errorIndex))
        {
            // The API is indicating an error.
           =20
            printf("error on SnmpMgrRequest %d\n", GetLastError());
        }
        else
        {
            // The API succeeded, errors may be indicated from the =
remote
            // agent.           =20
            switch(errorStatus)
            {
            case SNMP_ERRORSTATUS_TOOBIG:
                {
                    printf("SNMP_ERRORSTATUS_TOOBIG");
                    break;
                }
            case SNMP_ERRORSTATUS_NOSUCHNAME:
                {
                    printf("SNMP_ERRORSTATUS_NOSUCHNAME");
                    break;
                }
            case SNMP_ERRORSTATUS_BADVALUE:
                {
                    printf("SNMP_ERRORSTATUS_BADVALUE");
                    break;
                }
            default:
                break;
            }           =20
            if (errorStatus > 0)
            {
                printf("Error: errorStatus=3D%d, errorIndex=3D%d\n",
                    errorStatus, errorIndex);
            }
            else
            {
                // Display the resulting variable bindings.
               =20
                UINT i;
                char *string =3D NULL;
               =20
                for(i=3D0; i < variableBindings.len; i++)
                {
                    SnmpMgrOidToStr(&variableBindings.list[i].name, =
&string);
                    printf("Variable =3D %s\n", string);
                    if (string) SNMP_free(string);
                   =20
                    printf("Value    =3D ");
                    =
SnmpUtilPrintAsnAny(&variableBindings.list[i].value);
                   =20
                    printf("\n");
                } // end for()
            }
           =20
            // Free the variable bindings that have been allocated.      =
     =20
            SnmpUtilVarBindListFree(&variableBindings);
        }
    }
    else if (operation =3D=3D WALK)
    {
        // Walk is a common term used to indicate that all MIB variables
        // under a given OID are to be traversed and displayed.  This is
        // a more complex operation requiring tests and looping in =
addition
        // to the steps for get/getnext above.
       =20
       =20
        AsnObjectIdentifier root;
        AsnObjectIdentifier tempOid;
       =20
       =20
        SnmpUtilOidCpy(&root, &variableBindings.list[0].name);
       =20
        requestType =3D ASN_RFC1157_GETNEXTREQUEST;
       =20
       =20
        while(1)
        {
            if (!SnmpMgrRequest(session, requestType, =
&variableBindings,
                &errorStatus, &errorIndex))
            {
                // The API is indicating an error.
               =20
                printf("error on SnmpMgrRequest %d\n", GetLastError());
               =20
                break;
            }
            else
            {
                // The API succeeded, errors may be indicated from the =
remote
                // agent.
               =20
               =20
                // Test for end of subtree or end of MIB.
               =20
                if (errorStatus =3D=3D SNMP_ERRORSTATUS_NOSUCHNAME ||
                    SnmpUtilOidNCmp(&variableBindings.list[0].name,
                    &root, root.idLength))
                {
                    printf("End of MIB subtree.\n\n");
                   =20
                    break;
                }
               =20
               =20
                // Test for general error conditions or sucesss.
               =20
                if (errorStatus > 0)
                {
                    printf("Error: errorStatus=3D%d, errorIndex=3D%d =
\n",
                        errorStatus, errorIndex);
                   =20
                    break;
                }
                else
                {
                    // Display resulting variable binding for this =
iteration.
                   =20
                    char *string =3D NULL;
                   =20
                    SnmpMgrOidToStr(&variableBindings.list[0].name, =
&string);
                    printf("Variable =3D %s\n", string);
                    if (string) SNMP_free(string);
                   =20
                    printf("Value    =3D ");
                    =
SnmpUtilPrintAsnAny(&variableBindings.list[0].value);
                   =20
                    printf("\n");
                }
            } // end if()
           =20
           =20
            // Prepare for the next iteration.  Make sure returned oid =
is
            // preserved and the returned value is freed.
           =20
            SnmpUtilOidCpy(&tempOid, =
&variableBindings.list[0].name);
           =20
            SnmpUtilVarBindFree(&variableBindings.list[0]);
           =20
            SnmpUtilOidCpy(&variableBindings.list[0].name, =
&tempOid);
            variableBindings.list[0].value.asnType =3D ASN_NULL;
           =20
            SnmpUtilOidFree(&tempOid);
           =20
        } // end while()
       =20
       =20
        // Free the variable bindings that have been allocated.
       =20
        SnmpUtilVarBindListFree(&variableBindings);
       =20
        SnmpUtilOidFree(&root);
       =20
       =20
    }
    else if (operation =3D=3D TRAP)
    {
        // Trap handling can be done two different ways: event driven or
        // polled.  The following code illustrates the steps to use =
event
        // driven trap reception in a management application.
       =20
       =20
        HANDLE hNewTraps =3D NULL;
       =20
       =20
        if (!SnmpMgrTrapListen(&hNewTraps))
        {
            printf("error on SnmpMgrTrapListen %d\n", GetLastError());
        }
        else
        {
            printf("snmputil: listening for traps...\n");
        }
       =20
       =20
        while(1)
        {
            DWORD dwResult;
           =20
            if ((dwResult =3D WaitForSingleObject(hNewTraps, =
0xffffffff))
                =3D=3D 0xffffffff)
            {
                printf("error on WaitForSingleObject %d\n",
                    GetLastError());
            }
            else if (!ResetEvent(hNewTraps))
            {
                printf("error on ResetEvent %d\n", GetLastError());
            }
            else
            {
                AsnObjectIdentifier enterprise;
                AsnNetworkAddress   IPAddress;
                AsnInteger          genericTrap;
                AsnInteger          specificTrap;
                AsnTimeticks        timeStamp;
                RFC1157VarBindList  variableBindings;
               =20
                UINT i;
                char *string =3D NULL;
               =20
                while(SnmpMgrGetTrap(&enterprise, &IPAddress, =
&genericTrap,
                    &specificTrap, &timeStamp, =
&variableBindings))
                {
                    printf("snmputil: trap generic=3D%d =
specific=3D%d\n",
                        genericTrap, specificTrap);
                    if (IPAddress.length =3D=3D 4) {
                        printf("  from -> %d.%d.%d.%d\n",
                            (int)IPAddress.stream[0], =
(int)IPAddress.stream[1],
                            (int)IPAddress.stream[2], =
(int)IPAddress.stream[3]);
                       =20
                    }
                    if (IPAddress.dynamic) {
                        SNMP_free(IPAddress.stream);
                    }
                   =20
                    for(i=3D0; i < variableBindings.len; i++)
                    {
                        =
SnmpMgrOidToStr(&variableBindings.list[i].name, &string);
                        printf("Variable =3D %s\n", string);
                        if (string) SNMP_free(string);
                       =20
                        printf("Value    =3D ");
                        =
SnmpUtilPrintAsnAny(&variableBindings.list[i].value);
                    } // end for()
                    printf("\n");
                   =20
                   =20
                    SnmpUtilOidFree(&enterprise);
                   =20
                    SnmpUtilVarBindListFree(&variableBindings);
                }
            }
        } // end while()
       =20
       =20
    } // end if(operation)
   =20
   =20
    if (operation !=3D TRAP)
    {
        // Close SNMP session with the remote agent.
       =20
        if (!SnmpMgrClose(session))
        {
            printf("error on SnmpMgrClose %d\n", GetLastError());
           =20
            return 1;
        }
    }
   =20
   =20
    // Let the command interpreter know things went ok.
   =20
    return 0;
   =20
   =20

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -