SNMP Code Example: Interpret a MAC or IP Address Value
SNMP agent responses may return MAC or IP addresses in binary form rather than as readable strings. To correctly interpret these values, access the binary data and convert it into a human-readable format. The examples below show how to retrieve MAC or IP address values from SNMP responses by using the ValueB field and converting each byte to its hexadecimal representation.
If a response from an agent contains a MAC or IP address, use the ValueB field of the Objects collection instead of the Value field. For instance:
.NET
SNMPObject obj = new SNMPObject();
obj.Oid = "1.3.6.1.2.1.2.2.1.6";
snmpmgr1.Objects.Add(obj);
snmpmgr1.SendGetRequest();
string MACAddress = null;
for (int i = 0; i < snmpmgr1.Objects[0].ValueB.Length; i++)
{
MACAddress += snmpmgr1.Objects[0].ValueB[i].ToString("X") + " ";
}
Delphi
var MACAddress : String;
var i : Integer;
var response : RawByteString;
begin
SNMPMgr1.ObjCount := 1;
SNMPMgr1.ObjId[0] := '1.3.6.1.2.1.2.2.1.6';
SNMPMgr1.SendGetRequest();
response := SNMPMgr1.ObjValueB[0];
for i:= 0 to Length(response) do
begin
MACAddress := MACAddress + ' ' + IntToHex(ord(response[i]),2);
end;
ShowMessage(MACAddress);
end;
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.