如何指定设备IP地址,OID和社区字符串?

时间:2018-07-31 18:12:50

标签: c++ qt snmp

我正在Qt中构建一个snmpget函数,我发现了一些可能有效的代码。我唯一的问题是我不知道如何指定所需的参数。具体来说,是OID,设备IP地址和社区字符串。它可以按原样编译,但我不知道运行时它是否实际上在做任何事情。

似乎正在寻找OID和Community字符串,但即使如此,似乎也没有参数说明要寻找哪个设备。

我该如何做?

这是我的代码,

#include "snmpsession.h"
#include <QList>
#include <QMessageBox>
#include "QThread.h"

//----[ Constructors/Destructors ]-----------------------------------------------------

SNMPSession::SNMPSession()
        : QObject()
{
}

/**
*   The constructor will initialize the SNMPSession object with the given values,
*   and will also bind the UDP socket to the specified socketPort.
*   This allows the socket to emit the readyRead signal on datagram arrival.
*/
SNMPSession::SNMPSession(const QString &agentAddress, qint16 agentPort, qint16 socketPort)
        : QObject()
{
    this->agentAddress = new QHostAddress(agentAddress);
    this->agentPort = agentPort;
    this->socketPort = socketPort;
    udpSocket.bind(socketPort);
}

SNMPSession::~SNMPSession()
{
    delete agentAddress;
}


//----[ Get methods ]----------------------------------------------------------------




QHostAddress * SNMPSession::getAgentAddress() const
{

    return agentAddress;
}
qint16 SNMPSession::getAgentPort() const
{
    return agentPort;
}

qint16 SNMPSession::getSocketPort() const
{
    return socketPort;
}


//----[ SNMP get-request methods ]-------------------------------------------------


/**
*   This method will send a SNMP get-request with the specified community string and OID
*   and then place the received get-response value in the receivedValue variable.
*   Returns 0 on success or one of the following error codes on failture :
*   1 -- Response message too large to transport
*   2 -- The name of the requested object was not found
*   3 -- A data type in the request did not match the data type in the SNMP agent
*   4 -- The SNMP manager attempted to set a read-only parameter
*   5 -- General Error (some error other than the ones listed above)
*   6 -- Timeout, no response from agent (5 seconds)
*/
int SNMPSession::sendGetRequest(QString &receivedValue,
                                const QString &communityStringParameter, const QString &oidParameter)
{


    QByteArray communityString = communityStringParameter.toLatin1();
    QByteArray oid = oidParameter.toLatin1();

    QByteArray datagram; // the datagram to send
    QByteArray receivedDatagram; // the received datagram

    const int errorIndex = 14 + communityString.size();
    const int valueTypeIndex = 24 + oid.split('.').size() + communityString.size() + 1;
    const int valueLenghtIndex = 24 + oid.split('.').size() + communityString.size() + 2;
    const int valueIndex = 24 + oid.split('.').size() + communityString.size() + 3;

// include Value field
    int currentLength = 2;
    datagram.push_front((char)0x0);
    datagram.push_front(0x05);

    convertOIDAccordingToBER(oid);

// include Object Identifier field
    datagram.push_front(oid);
    currentLength += oid.size();
    datagram.push_front(oid.size());
    datagram.push_front(0x6);
    currentLength += 2;

// include Varbind field
    datagram.push_front(currentLength);
    datagram.push_front(0x30);
    currentLength += 2;

// include Varbind List field
    datagram.push_front(currentLength);
    datagram.push_front(0x30);
    currentLength += 2;

// include Error Index field
    datagram.push_front((char)0x0);
    datagram.push_front(0x1);
    datagram.push_front(0x2);
    currentLength += 3;
// include Error field
    datagram.push_front((char)0x0);
    datagram.push_front(0x1);
    datagram.push_front(0x2);
    currentLength += 3;

// include Request ID
    datagram.push_front((rand() % 100 + 1));
    datagram.push_front(0x1);
    datagram.push_front(0x2);
    currentLength += 3;

// include PDU field
    datagram.push_front(currentLength);
    datagram.push_front(0xA0);
    currentLength += 2;

// include Community string
    datagram.push_front(communityString);
    datagram.push_front(communityString.size());
    datagram.push_front(0x04);
    currentLength += communityString.size();
    currentLength += 2;

// include SNMP version
    datagram.push_front((char)0x0);
    datagram.push_front(0x01);
    datagram.push_front(0x02);
    currentLength += 3;

// finish the construction with the SNMP Message lenght and type code
    datagram.push_front(currentLength);
    datagram.push_front(0x30);

// SEND IT !!!
    udpSocket.writeDatagram(datagram, datagram.size(), *agentAddress, agentPort);

// Receive it
    int result = 0;

    int timeoutTimer = 0;
    while(timeoutTimer <= 3000)
    {
        if(udpSocket.hasPendingDatagrams())
        {
            return result = getValueFromGetResponse(receivedValue, receivedDatagram, errorIndex,
                                            valueTypeIndex, valueIndex, valueLenghtIndex);
        }

        QThread::msleep(1);
        timeoutTimer++;
    }

    // sending second attempt
    udpSocket.writeDatagram(datagram, datagram.size(), *agentAddress, agentPort);
    timeoutTimer = 0;

    while(timeoutTimer <= 3000)
    {
        if(udpSocket.hasPendingDatagrams())
        {
            return result = getValueFromGetResponse(receivedValue, receivedDatagram, errorIndex,
                                            valueTypeIndex, valueIndex, valueLenghtIndex);
        }

        QThread::msleep(1);
        timeoutTimer++;
    }

    // sending third attempt
    udpSocket.writeDatagram(datagram, datagram.size(), *agentAddress, agentPort);
    timeoutTimer = 0;

    while(timeoutTimer <= 500)
    {
        if(udpSocket.hasPendingDatagrams())
        {
            return result = getValueFromGetResponse(receivedValue, receivedDatagram, errorIndex,
                                            valueTypeIndex, valueIndex, valueLenghtIndex);
        }

        QThread::msleep(1);
        timeoutTimer++;
    }

    return 6;
}

0 个答案:

没有答案