Reputation: 507
I am trying to retrieve the MAC address of a device that connects to the network. My goal is to perform a WALK and then search the results by the port number that triggered the event.
I first grab the port information via a GetRequestMessage (Success). Then I TRY to perform a walk to get the MAC Address table. I do not get any errors or exceptions, but I also do not get any results.
Where am I going wrong?
// 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();
EDIT
Also, I am using this resource as guidance.
Upvotes: 1
Views: 2500
Reputation: 507
So I found the issue, which was two fold.
The first was when instantiating the Discovery for the BulkWalk, it needs to be as follows:
Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetBulkRequestPdu);
The key part being getting the SnmpType correct (my code above is SnmpType.GetRequestPdu and not SnmpType.GetBulkRequestPdu). The tutorial does not mention that the SnmpType is different.
Second, when passing parameters into the Messenger.BulkWalk() method I was passing in the community name and not the user name. The source code remarks for the BulkWalk method does say community name but it should be the user.
I did as Lex Li suggested and complied/ran the snmpwalk sample to verify that there were no issues. After that was a success, I went back and reviewed the source code for that sample and found the two issues.
Upvotes: 1