【问题标题】:SNMP agent configuration windows/linuxSNMP代理配置windows/linux
【发布时间】:2015-04-25 11:18:55
【问题描述】:

我需要通过 snmp 检索信息,然后使用这些信息在 Unity 中创建某种图形应用程序。

我找到了 SnmpSharp 库 http://www.snmpsharpnet.com/

我使用这个库统一创建了一个小程序,然后我在我的 windows 机器上安装了 snmp(使用 windows 官方指南),在 localhost 上它可以工作!

现在我的问题是 如何连接到我的局域网上的其他代理? 如何在我的其他设备(例如 linux pc)上安装代理? 我有点困惑,因为我尝试在另一台 Windows 电脑上安装 snmp,但我无法从中检索 snmp 信息;我尝试在 linux pc 上安装 snmp 代理,但我不明白如何正确安装必须跨局域网通信的代理

这是在本地主机上工作的代码

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 ();
}

}

感谢您的帮助,对不起我的英语!

【问题讨论】:

    标签: linux snmp net-snmp snmpd


    【解决方案1】:

    默认情况下,一旦安装,Windows 上的 SNMP 代理只允许来自本地主机的查询。

    要允许远程 SNMP 查询,您需要设置 SNMP 服务:

    • 打开Windows Services Manager

    • 右键点击SNMP Service并编辑Properties

    • 选择Security标签

    从这里,添加允许查询您的 SNMP 代理的远程主机。


    编辑:

    我知道这只能回答您的部分问题: 无法从 localhost 以外的主机查询 Windows SNMP 代理。

    如何在 Linux 上安装/配置 SNMP 代理将是另一个完整的问题,描述您尝试过的内容以及无效的内容。

    C# 代码审查/调试应发布在 SF 姐妹网站:stackoverflow

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多