使用Sharpsnmplib散步法执行SNMP步行



我正在尝试检索连接到网络的设备的MAC地址。我的目标是进行散步,然后通过触发事件的端口号搜索结果。

我首先通过getRequestMessage获取端口信息(成功)。然后,我尝试执行步行以获取MAC地址表。我没有任何错误或例外,但也没有得到任何结果。

我要去哪里?

// Capture IPAddress
string ipAddress = e.Sender.Address.ToString();
// Capture the port
string port = e.Message.Scope.Pdu.Variables[0].Data.ToString();
// Setup Authentication with password from App.Config
var auth = new SHA1AuthenticationProvider(new OctetString(_Password));
// Setup Privacy with Phrase from App.Config
var priv = new DESPrivacyProvider(new OctetString(_Phrase), auth);
// Setup username from App.Config
OctetString userName = new OctetString(_Username);
// Create IPEndPoint
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), 161);
// Create discovery
Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
// Create report
ReportMessage report = discovery.GetResponse(60000, iPEndPoint);
// Setup OID variables to get port information
List<Variable> variables = new List<Variable>
    {
        // Port Description
        new Variable(new ObjectIdentifier($"1.3.6.1.2.1.31.1.1.1.18.{ port }")),
        // Port PVID
        new Variable(new ObjectIdentifier($"1.3.6.1.2.1.17.7.1.4.5.1.1.{ port }")),
        // Voice VLAN
        new Variable(new ObjectIdentifier($"1.3.6.1.4.1.674.10895.5000.2.6132.1.1.1.2.13.1.28.{ port }")),
    };
// Create SNMP request
GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, userName, variables, priv, Messenger.MaxMessageSize, report);
// Send request and get reply
ISnmpMessage reply = request.GetResponse(60000, iPEndPoint);
// Request failed
if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
{
    throw ErrorException.Create(
        "error in response",
        IPAddress.Parse(ipAddress),
        reply);
}
// Store reply information
string Port_Description = reply.Scope.Pdu.Variables[0].Data.ToString(),
    Port_Pvid = reply.Scope.Pdu.Variables[1].Data.ToString(),
    Port_VLAN = reply.Scope.Pdu.Variables[2].Data.ToString();
// Create BulkWalk Discovery 
// TODO: Do I need to do this or can I reuse the original discovery??
Discovery BULKWALK_discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
// Create BulkWalk report
// TODO: Do I need to do this or can I reuse the original report??
ReportMessage BULKWALK_report = BULKWALK_discovery.GetResponse(60000, iPEndPoint);
// Variable to store the results of the WALK
var BULKWALK_results = new List<Variable>();
// Perform the walk and return the count of results. Community name from App.Config
var BULKWALK_results_count = Messenger.BulkWalk(VersionCode.V3, iPEndPoint, new OctetString(_CommunityName), OctetString.Empty, new ObjectIdentifier($"1.3.6.1.2.1.17.7.1.2.2.1.2"), BULKWALK_results, 60000, 10, WalkMode.WithinSubtree, priv, BULKWALK_report);
Debugger.Break();

编辑

另外,我正在使用此资源作为指导。

,所以我找到了问题,这是两个折。

首先是实例化散步的发现时,需要如下:

Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetBulkRequestPdu);

关键部分是使SNMPTYPE正确(我的上面的代码是snmptype.getRequestpdu,而不是snmptype.get bulk requestpdu)。本教程没有提到snmptype不同。

第二,当将参数传递到Messenger.bulkwalk()方法时,我以社区名称而不是用户名传递。Bulkwalk方法的源代码评论确实说明了社区名称,但应该是用户。

我所做的像Lex Li建议并遵守/遵守SNMPWALK样本,以验证没有问题。之后,成功了,我回去审查了该样本的源代码,并找到了两个问题。

最新更新