public static SecureAgentParameters GetSNMPV3Param(UdpTarget target, string communityString, securityModel secModel) { SecureAgentParameters _param = new SecureAgentParameters(); if (!target.Discovery(_param)) { return(null); } switch (secModel.securityLevel) { case NetworkHelper.securityLvl.noAuthNoPriv: _param.noAuthNoPriv(communityString); break; case NetworkHelper.securityLvl.authNoPriv: if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.MD5) { _param.authNoPriv(communityString, AuthenticationDigests.MD5, SNMP_AUTH_SECRET); } else if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.SHA) { _param.authNoPriv(communityString, AuthenticationDigests.SHA1, SNMP_AUTH_SECRET); } break; case NetworkHelper.securityLvl.authPriv: if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.MD5 && secModel.privacyProtocol == NetworkHelper.snmpProtocols.AES) { _param.authPriv(SNMP_COMMUNITY_STRING, AuthenticationDigests.MD5, SNMP_AUTH_SECRET, PrivacyProtocols.AES128, SNMP_PRIV_SECRET); } else if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.MD5 && secModel.privacyProtocol == NetworkHelper.snmpProtocols.DES) { _param.authPriv(SNMP_COMMUNITY_STRING, AuthenticationDigests.MD5, SNMP_AUTH_SECRET, PrivacyProtocols.DES, SNMP_PRIV_SECRET); } else if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.SHA && secModel.privacyProtocol == NetworkHelper.snmpProtocols.AES) { _param.authPriv(SNMP_COMMUNITY_STRING, AuthenticationDigests.SHA1, SNMP_AUTH_SECRET, PrivacyProtocols.AES128, SNMP_PRIV_SECRET); } else if (secModel.authentificationProtocol == NetworkHelper.snmpProtocols.SHA && secModel.privacyProtocol == NetworkHelper.snmpProtocols.DES) { _param.authPriv(SNMP_COMMUNITY_STRING, AuthenticationDigests.SHA1, SNMP_AUTH_SECRET, PrivacyProtocols.DES, SNMP_PRIV_SECRET); } break; } return(_param); }
private bool DiscoverAgents(IPAddress cameraAddress) { bool result = false; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); var test = ping.Send(cameraAddress); UdpTarget target = new UdpTarget(cameraAddress, 161, 5000, 3); SecureAgentParameters param = new SecureAgentParameters(); if (!target.Discovery(param)) { Console.WriteLine("Discovery failed. Unable to continue..."); target.Close(); } else { param.SecurityName.Set("mySecureName"); param.Authentication = AuthenticationDigests.MD5; param.AuthenticationSecret.Set("alstom1!"); param.Privacy = PrivacyProtocols.None; param.Reportable = false; SnmpV3Packet testResult; try { testResult = (SnmpV3Packet)target.Request(this.CreatePDU(PduType.Get), param); result = true; } catch (Exception ex) { Console.WriteLine(ex); testResult = null; result = false; } } return(result); //param.SecurityName.Set("mySecureName"); }
private System.Collections.Generic.Dictionary <string, string> SendPacket(SnmpOperationType pduType, System.Collections.Generic.List <VarBinding> variables) { if (variables == null || variables.Count < 1) { throw new System.ArgumentNullException("The variables for the " + pduType + " operation is null or empty."); } UdpTarget udpTarget = null; IAgentParameters agentParameters = null; Pdu pdu = null; System.Collections.Generic.Dictionary <string, string> result3; try { udpTarget = new UdpTarget(System.Net.IPAddress.Parse(this.config.AgentIp), this.config.Port, this.config.Timeout, this.config.Retry); if (this.config.Version == SnmpVersionType.Ver3) { agentParameters = new SecureAgentParameters(); SecureAgentParameters secureAgentParameters = agentParameters as SecureAgentParameters; if (!udpTarget.Discovery(secureAgentParameters)) { throw new SnmpException("Discovery failed: The device with ip(" + this.config.AgentIp + ") is unreachable."); } pdu = new ScopedPdu(); SnmpV3Config snmpV3Config = this.config as SnmpV3Config; secureAgentParameters.SecurityName.Set(snmpV3Config.UserName); secureAgentParameters.Authentication = (AuthenticationDigests)snmpV3Config.Authentication; secureAgentParameters.AuthenticationSecret.Set(snmpV3Config.AuthSecret); secureAgentParameters.Privacy = (PrivacyProtocols)snmpV3Config.Privacy; secureAgentParameters.PrivacySecret.Set(snmpV3Config.PrivacySecret); secureAgentParameters.Reportable = true; } else { if (this.config.Version == SnmpVersionType.Ver1) { OctetString community = new OctetString(((SnmpV1Config)this.config).Community); agentParameters = new AgentParameters(SnmpVersion.Ver1, community); } else { OctetString community = new OctetString(((SnmpV2Config)this.config).Community); agentParameters = new AgentParameters(SnmpVersion.Ver2, community); } pdu = new Pdu(); } DictionaryUtil dictionaryUtil = new DictionaryUtil(); foreach (VarBinding current in variables) { try { if (current is LeafVarBinding) { if (pduType.Equals(SnmpOperationType.GetTable) || pduType.Equals(SnmpOperationType.Walk)) { pdu.Type = PduType.Get; } else { pdu.Type = (PduType)pduType; if (pduType.Equals(SnmpOperationType.GetBulk)) { this.configBulkPdu(pdu, current.MaxRepetition); } } System.Collections.Generic.Dictionary <string, string> result = this.ReceiveResponseWithLeafVB((LeafVarBinding)current, pdu, udpTarget, agentParameters); dictionaryUtil.Add(result); } else { if (agentParameters.Version == SnmpVersion.Ver1) { pdu.Type = PduType.GetNext; } else { pdu.Type = PduType.GetBulk; this.configBulkPdu(pdu, current.MaxRepetition); } System.Collections.Generic.Dictionary <string, string> result2 = this.ReceiveResponseWithTableVB((TableVarBinding)current, pdu, udpTarget, agentParameters); dictionaryUtil.Add(result2); } } catch (System.Exception ex) { if (!ex.Message.Contains("Invalid ASN.1 type encountered 0x00. Unable to continue decoding.")) { throw new SnmpException(ex.Message); } } } result3 = dictionaryUtil.Result; } catch (System.Exception ex2) { throw new SnmpException(ex2.Message); } finally { if (udpTarget != null) { udpTarget.Close(); } } return(result3); }