/*---------------------------------------

SNMP信息刺探程序

----------------------------------------*/

#include <stdio.h>

#include <malloc.h>

#include <snmp.h>

#include <mgmtapi.h>

#pragma comment(lib,"Mgmtapi.lib")

#pragma comment(lib,"Snmpapi.lib")

//利用 SNMP API时需要以上头文件和库文件

#define GET     1  //get,就理解成获取一个信息。

#define GETNEXT 2  //getnext,就理解成获取下一个信息。

#define WALK    3  //walk,就理解成获取一堆信息,即所有数据库子树/子目录的信息

#define TIMEOUT 6000 /* milliseconds */

#define RETRIES 3

//一些有用的oid

char *SnmpOid[5]={".1.3.6.1.2.1.25.4.2.1.2",//进程列表

                ".1.3.6.1.4.1.77.1.2.25.1.1",//系统用户

    ".1.3.6.1.4.1.77.1.4.1.0",//域名

    ".1.3.6.1.2.1.25.6.3.1.2",//列出安装的软件

    ".1.3.6.1.2.1.1"};// 列出系统信息

void usage(char *name)

{

printf("=================SNMP tool================/n");

printf("​​=======gxisone@hotmail.com​​   2004/8/10====/n");

printf("/nusage: %s [remoteip] [sysprocess|sysuser|domainname|sysinf|software]/n",name);

printf("Exameple: %s 192.168.1.1 sysuser/n",name);

}

int main(int  argc,char *argv[])

    {

    int                operation;

    LPSTR              agent;

    LPSTR              community;


    RFC1157VarBindList variableBindings;

    LPSNMP_MGR_SESSION session;

    int        timeout = TIMEOUT;

    int        retries = RETRIES;

    int i;

    BYTE       requestType;

    AsnInteger errorStatus;

    AsnInteger errorIndex;

    char        *chkPtr = NULL;

    operation = WALK;  //这个程序使用WALK来获取信息

    if  (argc != 3)

        {

        usage(argv[0]);

        return 0;

        }

   else

          {

  AsnObjectIdentifier reqObject;

        // 取得IP地址

  agent = (LPSTR)SNMP_malloc(strlen(*argv) + 1);

  strcpy(agent, argv[1]);


        community="public";//设置查询密码


        variableBindings.list = NULL;

        variableBindings.len = 0;

  // 设置 oid

  if(!strcmp(argv[2],"sysprocess"))i=0;

  else if(!strcmp(argv[2],"sysuser"))i=1;

  else if(!strcmp(argv[2],"domainname"))i=2;

  else if(!strcmp(argv[2],"software"))i=3;

  else if(!strcmp(argv[2],"sysinf"))i=4;

  else{

   usage(argv[0]);

   return 0;

  }

printf("%s/n",SnmpOid[i]);


            // 把字符串转换成标准oid

     if (!SnmpMgrStrToOid(SnmpOid[i], &reqObject))

        {

         printf("Error: Invalid oid, %s, specified./n", *argv);

         return 1;

        }

     else

       {

       variableBindings.len++;

   if ((variableBindings.list = (RFC1157VarBind *)SNMP_realloc(

                   variableBindings.list, sizeof(RFC1157VarBind) *

                   variableBindings.len)) == NULL)

      {

         printf("Error: Error allocating oid, %s./n",*argv);

         return 1;

      }

 variableBindings.list[variableBindings.len - 1].name=reqObject;

 variableBindings.list[variableBindings.len - 1].value.asnType=ASN_NULL;

     }

   // Make sure only one variable binding was specified if operation

   // is WALK.

        if (operation == WALK && variableBindings.len != 1)

            {

            printf("Error: Multiple oids specified for WALK./n");

            return 1;

            }

        // Establish a SNMP session to communicate with the remote agent.  The

        // community, communications timeout, and communications retry count

        // for the session are also required.

if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL)

     {

      printf("error on SnmpMgrOpen %d/n", GetLastError());

      return 1;

      }

   } // end if

 {


        AsnObjectIdentifier root;

        AsnObjectIdentifier tempOid;

        SnmpUtilOidCpy(&root, &variableBindings.list[0].name);

        requestType = ASN_RFC1157_GETNEXTREQUEST;

        for(;;)

            {

            if (!SnmpMgrRequest(session, requestType, &variableBindings,

                                &errorStatus, &errorIndex))

                {

                printf("error on SnmpMgrRequest %d/n", GetLastError());

                break;

                }

            else

                {


   if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ||

          SnmpUtilOidNCmp(&variableBindings.list[0].name,

                                 &root, root.idLength))

              {

              printf("End of MIB subtree./n/n");

           break;

       }

    if (errorStatus > 0)

   {

   printf("Error: errorStatus=%d, errorIndex=%d /n", errorStatus, errorIndex);

   break;

   }

   else

      {

      // 打印查询的结果

      char *string = NULL;

     SnmpMgrOidToStr(&variableBindings.list[0].name, &string);

     printf("Variable = %s/n", string);

     if (string) SNMP_free(string);

      printf("Value    = ");

      SnmpUtilPrintAsnAny(&variableBindings.list[0].value);

      printf("/n");

     }

} // end if()

// 准备下一次查询

  SnmpUtilOidCpy(&tempOid, &variableBindings.list[0].name);

  SnmpUtilVarBindFree(&variableBindings.list[0]);

  SnmpUtilOidCpy(&variableBindings.list[0].name, &tempOid);

  variableBindings.list[0].value.asnType = ASN_NULL;

  SnmpUtilOidFree(&tempOid);

} // end while()

   // 释放资源

   SnmpUtilVarBindListFree(&variableBindings);

   SnmpUtilOidFree(&root);

}

  // 关闭 SNMP session

  if (!SnmpMgrClose(session))//清理退出

    {

     printf("error on SnmpMgrClose %d/n", GetLastError());

     return 1;

    }


  return 0;

 }