linuxsnmpnet-snmpsnmpd

SNMP agent configuration windows/linux


i need to retrieve information via snmp and then use this information to create some sort of graphic application in Unity.

I find the SnmpSharp library http://www.snmpsharpnet.com/

i create a little program in unity using this library and then i installed the snmp on my windows machine(using windows official guide) , on localhost it works!

Now my problem is how can i connect to other agent on my lan network ? How can install agent on my other device example a linux pc ? i'm a little confused beacuse i try to install snmp on an other windows pc but i can't retrieve snmp information from it ; i try to install the snmp agent on a linux pc but i don't understand how to correctly install an agent that have to communicate across my lan

This is the code that work on localhost

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using SnmpSharpNet;
using System.Net;


public class SNMP_walk : MonoBehaviour {

[SerializeField] public InputField community_str;
[SerializeField] public InputField agent_ip_address;
[SerializeField] public Button MyButton ;
string str_community  = null;
string ip  = null;
void Start () {
    //test button

    MyButton.onClick.AddListener (() => {
        str_community = community_str.text; 
        ip = agent_ip_address.text;
        Debug.Log (str_community);
        Debug.Log(ip);
        snmp_walk (str_community, ip);
    });
}

// Update is called once per frame
void Update () {

}

void snmp_walk(string str_community, string ip){

    OctetString community = new OctetString (str_community);
    // Define agent parameters class
    AgentParameters param = new AgentParameters (community);
    // Set SNMP version to 1 (or 2)
    // settare in base alla versione usata
    param.Version = SnmpVersion.Ver1;
    // Construct the agent address object
    // IpAddress class is easy to use here because
    //  it will try to resolve constructor parameter if it doesn't *
    //  parse to an IP address
    IpAddress agent = new IpAddress (ip);

    // Construct target
    // IP, port,timeout,retry
    UdpTarget target = new UdpTarget ((IPAddress)agent, 161, 4000, 2);
    //necessario per tutte le richieste
    Pdu pdu = new Pdu (PduType.Get);

    pdu.VbList.Add ("1.3.6.1.2.1.1.1.0"); //sysDescr
    pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
    pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
    pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
    pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName
    pdu.VbList.Add ("1.3.6.1.2.1.25.2.2.0"); //Load for one core
    //pdu.VbList.Add ("1.3.6.1.2.1.25.6.3.1.1.11"); //Memory ? occupata
    //pdu.VbList.Add ("1.3.6.1.2.1.25.3.3.1.2.4"); //CPU ?

    SnmpV1Packet result = (SnmpV1Packet)target.Request (pdu, param);


    // If result is null then agent didn't reply or we couldn't parse the reply.
    if (result != null) {
        // ErrorStatus other then 0 is an error returned by 
        // the Agent - see SnmpConstants for error definitions
        if (result.Pdu.ErrorStatus != 0) {
            // agent reported an error with the request
            Debug.Log ("Error in SNMP reply. Error {"+result.Pdu.ErrorStatus+"} " +
                       "index {"+result.Pdu.ErrorIndex+"}");

        } else {
            // Reply variables are returned in the same order as they were added
            //  to the VbList
            Debug.Log ("sysDescr({"+result.Pdu.VbList [0].Oid.ToString ()+"}) " +
                "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [0].Value.Type)+"}): " +
                "{"+result.Pdu.VbList [0].Value.ToString ()+"}"); 


            Debug.Log("sysObjectID({"+result.Pdu.VbList [1].Oid.ToString ()+"}) " +
                      "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [1].Value.Type)+"}): " +
                      "{"+result.Pdu.VbList [1].Value.ToString ()+"}"); 

            Debug.Log("sysUpTime(({"+result.Pdu.VbList [2].Oid.ToString ()+"}) " +
                      "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [2].Value.Type)+"}): " +
                      "{"+result.Pdu.VbList [2].Value.ToString ()+"}"); 

            Debug.Log("sysContact(({"+result.Pdu.VbList [3].Oid.ToString ()+"}) " +
                      "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [3].Value.Type)+"}): " +
                      "{"+result.Pdu.VbList [3].Value.ToString ()+"}"); 

            Debug.Log("sysName(({"+result.Pdu.VbList [4].Oid.ToString ()+"}) " +
                      "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [4].Value.Type)+"}): " +
                      "{"+result.Pdu.VbList [4].Value.ToString ()+"}"); 

            Debug.Log("Total Mem ({"+result.Pdu.VbList [5].Oid.ToString ()+"}) " +
                      "({"+SnmpConstants.GetTypeName (result.Pdu.VbList [5].Value.Type)+"}): " +
                      "{"+result.Pdu.VbList [5].Value.ToString ()+"}"); 

        /*  Debug.Log("CPU ? " + result.Pdu.VbList[6].Oid.ToString()+
                      SnmpConstants.GetTypeName(result.Pdu.VbList[6].Value.Type)+
                      result.Pdu.VbList[6].Value.ToString());*/

        }
    } else {
        Debug.Log ("No response received from SNMP agent.");
    }
    target.Close ();
}

}

Thanks for your help and sorry for my english !


Solution

  • By default, once installed, SNMP Agent on Windows only allows queries from localhost.

    To allow remote SNMP queries, you need to setup the SNMP Service :

    enter image description here

    From here, add remote host(s) allowed to query your SNMP Agent.


    Edit:

    I am aware that this could just answer a part of your question : Cannot query Windows SNMP Agent from hosts other than localhost.

    How to install/configure SNMP Agent on Linux would be a full other question, describing what you have tried and what is not working.

    C# code review/debug shoud be posted on SF sister site : stackoverflow