Vc ++ implements the SNMP information spying program

Source: Internet
Author: User

 

/*---------------------------------------
SNMP information spying program

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

# Include <stdio. h>
# Include <malloc. h>
# Include <snmp. h>
# Include <mgmtapi. h>
# Pragma comment (lib, "Mgmtapi. lib ")
# Pragma comment (lib, "Snmpapi. lib ")
// The above header file and library file are required when snmp api is used
# Define GET 1 // get, which is understood as obtaining an information.
# Define GETNEXT 2 // getnext, which is interpreted as obtaining the next information.
# Define WALK 3 // walk: Get a bunch of information, that is, all database subtree/subdirectory Information

# Define Time Out 6000/* milliseconds */
# Define RETRIES 3

// Some useful oid
Char * SnmpOid [5] = {". 1.3.6.1.2.1.25.4.2.1.2", // Process List
". 1.3.6.1.4.1.77.1.2.25.1.1", // system user
". 1.3.6.1.4.1.77.1.4.1.0", // Domain Name
". 1.3.6.1.2.1.25.6.3.1.2", // list the installed software
". 1.3.6.1.2.1.1"}; // list system information
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; // This program uses WALK to obtain information

If (argc! = 3)
{
Usage (argv [0]);
Return 0;
}
Else
{
AsnObjectIdentifier reqObject;
// Obtain the IP address
Agent = (LPSTR) SNMP_malloc (strlen (* argv) + 1 );
Strcpy (agent, argv [1]);

Community = "public"; // set the query Password

VariableBindings. list = NULL;
VariableBindings. len = 0;

// Set 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]);


// Convert the string into a standard 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.
// 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 ");
Break;
}

If (errorStatus> 0)
{
Printf ("Error: errorStatus = % d, errorIndex = % d/n", errorStatus, errorIndex );
Break;
}
Else
{
// Print the query result
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 ()
// Prepare for the next Query
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 ()
// Release resources

SnmpUtilVarBindListFree (& variableBindings );
SnmpUtilOidFree (& root );
}
// Close the SNMP session
If (! SnmpMgrClose (session) // clear and exit
{
Printf ("error on SnmpMgrClose % d/n", GetLastError ());
Return 1;
}

 
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.