I'm a beginner in SNMP.And I follow the examples of NET-SNMP-5.2.1, I
try to implented a function like this: I can get string of the target
snmpd from remote host and I can also send a command to make target
snmpd run a command .Here's the MIB:
MY-MIB DEFINITIONS ::=
BEGIN
IMPORTS
enterprises FROM RFC1155-SMI
OBJECT-TYPE FROM RFC-1212
DisplayString FROM RFC1213-MIB;
aa OBJECT IDENTIFIER ::= { enterprises 23000 }
bb OBJECT IDENTIFIER ::= { aa 1 }
cc OBJECT IDENTIFIER ::= { bb 1 }
runcmd OBJECT IDENTIFIER ::= { cc 1 }
-- runcmd group --
cmdStr OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..255))
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"the command string for running purpose."
::= {runcmd 1}
END
Here's my code:
/* include important headers */
#include <net-snmp/net-snmp-config.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
/* needed by util_funcs.h */
#if TIME_WITH_SYS_TIME
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <errno.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
/* header_generic() comes from here */
#include "util_funcs.h"
/* include our .h file */
#include "newmod.h"
#define NEWMOD_STR_LEN 1024
#define NEWMOD_STR_DEFAULT "argument for command script"
int newmod_int = 42;
char newmod_str[ NEWMOD_STR_LEN ];
/* Forward declarations for the config handlers */
void newmod_parse_config_newmodstr( const char *token, char *cptr );
void newmod_free_config_newmodstr( void );
/* newmod variable which is 3,so we choice the structure variable4 */
struct variable4 newmod_variables[] = {
{ NEWMODCMDSTR, ASN_OCTET_STR, RWRITE, var_newmod, 3, {1,1,1}}
};
oid newmod_variables_oid[] = { 1,3,6,1,4,1,23000,1 };
/* intialize the newmod module */
void init_newmod(void)
{
printf("init_newmod function \n");
REGISTER_MIB("newmod", newmod_variables, variable4,
newmod_variables_oid);
strcpy( newmod_str, NEWMOD_STR_DEFAULT );
snmpd_register_config_handler( "newmodstring",
newmod_parse_config_newmodstr,
newmod_free_config_newmodstr,
"newmodstring value" );
}
void
newmod_parse_config_newmodstr( const char *token, char *cptr )
{
if ( strlen( cptr ) < NEWMOD_STR_LEN )
strcpy( newmod_str, cptr );
else {
strncpy( newmod_str, cptr, NEWMOD_STR_LEN-3 );
newmod_str[ NEWMOD_STR_LEN-3 ] = 0;
strcat( newmod_str, "..." );
}
}
u_char *
var_newmod(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **write_method)
{
static char string[NEWMOD_STR_LEN]; /* for EXAMPLESTRING */
static oid oid_ret[8]; /* for EXAMPLEOBJECTID */
static long long_ret; /* for everything else */
if (header_generic(vp, name, length, exact, var_len, write_method) ==
MATCH_FAILED)
return NULL;
switch (vp->magic){
case NEWMODCMDSTR:
sprintf(string,newmod_str);
*var_len = strlen(string);
*write_method = write_newmodstr;
return (u_char *) string;
default:
printf("snmpd", "unknown sub-id %d in
examples/var_example\n", vp->magic);
}
return NULL;
}
int write_newmodstr(int action,
u_char *var_val,
u_char var_val_type,
size_t var_val_len,
u_char *statP,
oid *name,
size_t name_len)
{
/* Define an arbitrary maximum permissible value */
int len;
u_char *valp,*buf;
int pid = 0,ret = 0;
printf("now in the set function of wirte_newmodstr!\n");
len = var_val_len;
switch (action) {
case RESERVE1:
if (var_val_type != ASN_OCTET_STR) {
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > 1024) {
return SNMP_ERR_WRONGLENGTH;
}
*valp = *var_val;
break;
case RESERVE2:
break;
case FREE:
break;
case ACTION:
strcpy(*buf,newmod_str);
strcpy(newmod_str,*valp);
break;
case UNDO:
strcpy(newmod_str,*buf);
break;
case COMMIT:
break;
}
return SNMP_ERR_NOERROR;
}
void newmod_free_config_newmodstr( void )
{
}
Now I can use snmpget to get the string,but I can not set the string.
Once I run snmpset command in remote host, the target snmpd will
terminate automatically. Who can tell the error in my codes and tell
what does these mean such as RESERVE1,RESERVE2,UNDO,COMMIT,FREE,ACTION
in write mothod? And how can I run a command such as "ls -l" ?
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders