Login  
Search All Forums
Dart Home | PowerSNMP for .NET | Custom Development Reply | PowerSNMP for .NET Topics | Forums   
AuthorForum: PowerSNMP for .NET
Topic: Set source ip at broadcast message to discover agents
Avitt

From: netanya, Israel
Posts: 5
Member Since: 01/24/19
posted November 11, 2020 4:56 AM

Im trying to use the example of discovering agents from the docs:
http://dotnet.dart.com/help/psnet/Dart.Snmp~Dart.Snmp.SnmpSocket~GetResponses(RequestMessage).html

I need to discover SNMP V3 agents which the doc says they will likely to not respond but the do respond if i add the security object to the message, So I Modified the code to be:
    private void sendBroadcastGet(SnmpSocket managerSocket, object state)
    {
      Manager manager1 = new global::Dart.Snmp.Manager();
      manager1.Message += Manager1_Message;
      //Create Get message requesting a common OID
      GetMessage msg = new GetMessage();
      msg.Security.User.Name = "user";
      msg.Security.User.AuthenticationPassword = "AuthPassword";
      msg.Security.User.PrivacyPassword = "PrivPassword";
      msg.Security.User.PrivacyProtocol = PrivacyProtocol.Aes128;
      msg.Security.User.AuthenticationProtocol = AuthenticationProtocol.Sha;
      msg.Version = SnmpVersion.Three;

      msg.Variables.Add(new Variable("WorkingOid"));

      //Configure SnmpSocket.ReceiveTimeout to specify how long GetResponses() waits for responses here if needed.
      managerSocket.ReceiveTimeout = 5000;
      //managerSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.)
      ResponseMessage[] responses = managerSocket.GetResponses(msg);

      //Marshal responses back to UI thread for display
      manager1.Marshal(responses, "discovery", null);
    }

With the correct passwords and users of course and i do get the reply from the agent. My problem is that i have multiple ip`s on the NIC and also possibly multiple NICS and each of them have multiple ip`s

For testing i tried use only 1 NIC and 1 IP which is at the subnet of the agent IP and it worked.
But when having multiple IP`s its only send the message from the first IP and i don`t get reply from the agent.
When having multiple IP I tried using:
msg.Origin = new System.Net.IPEndPoint(IPAddress.Parse("requiredIp"), 161);
but the message is still sent from the first ip.
Also tried:
 System.Net.IPEndPoint ep = new System.Net.IPEndPoint(requiredIP, 161);
      managerSocket.Bind(ep);
but that gives invalid argument exception
I Use wireshark for debugging the issue
Nick B (Admin)

From: Utica, NY USA
Posts: 619
Member Since: 05/25/10

Extra Support Options
Custom Application Development

posted November 11, 2020 1:01 PM

Hello,

In order to specify the local IP, you should use the Manager.Start() method or SnmpSocket constructor:
http://dotnet.dart.com/help/psnet/webframe.html#Dart.Snmp~Dart.Snmp.SnmpBase~Start(ThreadStart,IPEndPoint,Object).html
http://dotnet.dart.com/help/psnet/webframe.html#Dart.Snmp~Dart.Snmp.SnmpSocket~_ctor(SnmpBase,IPEndPoint).html

It is also recommended to be using the Dart.Snmp.IPEndPoint class.

------
-Non-current subscribers must contact sales@dart.com to update subscription and receive continued support as needed.
------

Avitt

From: netanya, Israel
Posts: 5
Member Since: 01/24/19
posted November 12, 2020 4:00 AM

Thanks. I added the EndPoint at my manager.start method as suggested, For some reason the method 'sendBroadcastGet' is not being invoked at that case.
My change of code:
_snmpManager.Start(sendBroadcastGet,new Dart.Snmp.IPEndPoint(IPAddress.Parse("required address"),161), null);
Nick B (Admin)

From: Utica, NY USA
Posts: 619
Member Since: 05/25/10

Extra Support Options
Custom Application Development

posted November 13, 2020 12:38 PM

Hello,

That's unexpected. Do you have a try-catch around it possibly eating the exception, or is any exception being thrown? Do you have the Error event wired up, and is it being raised? If yes to any, what is the Exception.ToString()?

------
-Non-current subscribers must contact sales@dart.com to update subscription and receive continued support as needed.
------

Avitt

From: netanya, Israel
Posts: 5
Member Since: 01/24/19
posted November 15, 2020 2:28 AM

There is no exception
Here is my complete code (with placeholders for security) maybe there`s something wrong i`m missing.
When works with no setting source IP:

public void StartAgentDiscovery()
  {
    Dart.Snmp.License.Set("License");
      Manager _snmpManager = new Manager();
      try
      { 
       _snmpManager.Start(sendBroadcastGet, _snmpManager.Mib.CreateVariable(NodeName.sysUpTime));
      }
      catch(Exception e)
      {

      } 
    }

 private void sendBroadcastGet(SnmpSocket managerSocket, object state)
    {
      Manager manager1 = new global::Dart.Snmp.Manager();
      manager1.Message += Manager1_Message;
      GetMessage msg = new GetMessage();
      msg.Security.User.Name = "user";
      msg.Security.User.AuthenticationPassword = "authPAssword";
      msg.Security.User.PrivacyPassword = "privPassword";
      msg.Security.User.PrivacyProtocol = PrivacyProtocol.Aes128;
      msg.Security.User.AuthenticationProtocol = AuthenticationProtocol.Sha;
      msg.Version = SnmpVersion.Three;

      msg.Variables.Add(state as Variable);

      //Configure SnmpSocket.ReceiveTimeout to specify how long GetResponses() waits for responses here if needed.
      managerSocket.ReceiveTimeout = 5000;
      //managerSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.)
      ResponseMessage[] responses = managerSocket.GetResponses(msg);

      //Marshal responses back to UI thread for display
      manager1.Marshal(responses, "discovery", null);
    }

https://ibb.co/477XvN1

When not working with source IP:

    public void StartAgentDiscovery()
    {
      if (!File.Exists(filePath))
      {
        //_logger.Error("load mib file failed file path " + filePath);
        return;
      }
      Dart.Snmp.License.Set("License");
      Manager _snmpManager = new Manager();
      try
      {
        _snmpManager.Start(sendBroadcastGet, new Dart.Snmp.IPEndPoint(IPAddress.Parse("169.254.3.33"), 161), _snmpManager.Mib.CreateVariable(NodeName.sysUpTime));
      }
      catch(Exception e)
      {

      } 
    }

My IpConfig:
Ethernet adapter Ethernet:

  Connection-specific DNS Suffix . :
  Link-local IPv6 Address . . . . . : fe80::150:a5bb:31c5:b284%15
  IPv4 Address. . . . . . . . . . . : 10.10.12.121
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 10.10.12.254

Ethernet adapter Ethernet 4:

  Connection-specific DNS Suffix . :
  Link-local IPv6 Address . . . . . : fe80::7da0:96c1:27e0:b116%18
  IPv4 Address. . . . . . . . . . . : 192.168.1.101
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  IPv4 Address. . . . . . . . . . . : 169.254.3.33
  Subnet Mask . . . . . . . . . . . : 255.255.0.0
  Default Gateway . . . . . . . . . :

sendBroadcastGet is the same

Nothing on wireshark with that case,
sendBroadcastGet is not invoked (break point not reached)
Nick B (Admin)

From: Utica, NY USA
Posts: 619
Member Since: 05/25/10

Extra Support Options
Custom Application Development

posted November 16, 2020 4:27 PM

Hello,

Just to confirm this, as you do have a try-catch setup to eat any exception, if you Console.Write(e.ToString()) to the VS console within that catch in your code, you don't get an exception in the console? My largest suspicion is that something else is listening on 161, so an exception would be thrown that the port is in use and sendBroadcastGet wouldn't be called. Additionally, for GET operations, a 0 (the ephemeral port) is typically used for the port argument.

------
-Non-current subscribers must contact sales@dart.com to update subscription and receive continued support as needed.
------

Avitt

From: netanya, Israel
Posts: 5
Member Since: 01/24/19
posted November 17, 2020 2:25 AM

Indeed after reading the method documentation 'for GET operations, a 0 (the ephemeral port) is typically used for the port argument.' solved the issue.
changed my start method to:
_snmpManager.Start(sendBroadcastGet, new Dart.Snmp.IPEndPoint(IPAddress.Parse("169.254.3.33"), 0),
sendBroadcastGet is invoked and i get the reply from the agent.
There is still something weird, I dont see the messages on wireshark. The filter i use is
snmp || udp.port == 161 || udp.port == 162 || ip.src == <AgentIp> || ip.src == <LocalIp>

where AgentIp is the ip i get at the message.Origin, and the LocalIp is the one in using at the Start method
Reply | PowerSNMP for .NET Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX