Search code examples
network-programmingsnmp

Difficulty with netsnmp: using snmpget and logging errors to STDOUT


In the man page I see the following:

-L LOGOPTS        Toggle various defaults controlling logging:
              e:           log to standard error
              o:           log to standard output

This excites me very much because I'm in a situation where it would be advantageous for me to capture errors from STDOUT rather than from STDERR.

If I run the command:

snmpget -v1 -ccommString  -Lo 172.16.x.x  .1.2.3.4.5.6.7.8.9

I get back in my terminal

Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: iso.2.3.4.5.6.7.8.9

however if I run the command:

snmpget -v1 -ccommString  -Lo 172.16.x.x .1.2.3.4.5.6.7.8.9 2> foo

I get no response at the terminal but the file foo contains the same error message that I got above. So I am getting the error message on STDERR and not on STDOUT as I would expect.

Am I reading the man page wrong here? Additionally I have tried this on both linux and windows systems and because of the situation I'm in I would prefer to have the errors emitted on STDOUT rather than STDERR because I do not want to use the shell to do the work with the usual 2>&1

Any help or suggestions greatly appreciated.


Solution

  • The following c code is part of the source of the snmpget utility (snmpget.c in net-snmp-5.4.2.1\apps).

    fprintf(stderr, "Error in packet\nReason: %s\n",
            snmp_errstring(response->errstat));
    
    if (response->errindex != 0) {
        fprintf(stderr, "Failed object: ");
        for (count = 1, vars = response->variables;
                vars && count != response->errindex;
                vars = vars->next_variable, count++)
            /*EMPTY*/;
        if (vars) {
            fprint_objid(stderr, vars->name, vars->name_length);
    

    As you can see messages are simply written to stderr.

    You can have a custom snmpget utility: Download the source, replace stderr with stdout then recompile.