private string SendRequests(UdpTarget target, SecureAgentParameters param) { try { SnmpV3Packet indexResult = (SnmpV3Packet)target.Request(_index, param); SnmpV3Packet descrResult = (SnmpV3Packet)target.Request(_descr, param); SnmpV3Packet typeResult = (SnmpV3Packet)target.Request(_type, param); if (indexResult.ScopedPdu.ErrorStatus != 0 || descrResult.ScopedPdu.ErrorStatus != 0 || typeResult.ScopedPdu.ErrorStatus != 0) { Logger.Log("SNMP Request Error", Logger.MessageType.ERROR); return("<color><red>ERROR: packet error status = " + indexResult.ScopedPdu.ErrorStatus.ToString() + " and not ZERO"); } else { for (int i = 0; i < indexResult.ScopedPdu.VbCount; i++) { _output += "Interface " + indexResult.ScopedPdu.VbList[i].Value.ToString() + ": Type " + typeResult.ScopedPdu.VbList[i].Value.ToString() + " -> " + descrResult.ScopedPdu.VbList[i].Value.ToString() + "\n"; } Logger.Log("Successfully performed SNMP Device Request", Logger.MessageType.INFORM); return(_output); } } catch (Exception) { Logger.Log("SNMP Request Error", Logger.MessageType.ERROR); return("<color><red>ERROR: sending requests failed"); } }
private uint ParameterCountRequest(SecureAgentParameters param, UdpTarget target) { Oid deviceNr = new Oid(_ifNr); Pdu devices = new Pdu(PduType.Get); devices.VbList.Add(deviceNr); try { SnmpV3Packet devicesResult = (SnmpV3Packet)target.Request(devices, param); if (devicesResult.ScopedPdu.ErrorStatus != 0) { return(0); } else { return(Convert.ToUInt32(devicesResult.ScopedPdu.VbList[0].Value.ToString())); } } catch (Exception) { return(0); } }
private void ExecuteRequest(UdpTarget target, SecureAgentParameters param) { Oid _name = new Oid("1.3.6.1.2.1.1.5.0"); Pdu _namePacket = new Pdu(PduType.Get); _namePacket.VbList.Add(_name); try { SnmpV3Packet nameResult = (SnmpV3Packet)target.Request(_namePacket, param); if (nameResult.ScopedPdu.VbList[0].Value.ToString().ToLowerInvariant() == System.Environment.MachineName.ToLowerInvariant()) { Logger.Log("SNMP Service seems to work", Logger.MessageType.INFORM); outp.outState = JobOutput.OutState.Success; } else { Logger.Log("SNMP Service seems to be dead", Logger.MessageType.ERROR); outp.outState = JobOutput.OutState.Failed; } } catch (Exception) { Logger.Log("SNMP Service seems to be dead", Logger.MessageType.ERROR); outp.outState = JobOutput.OutState.Exception; } }
public override string Execute(int consoleWidth) { string _errorMessage = "<color><red>ERROR: "; try { ParseIP(); ParseParameters(); } catch (ArgumentException ex) { return(_errorMessage + ex.Message); } UdpTarget _target = new UdpTarget(_targetIP, 161, 5000, 3); if (version == 2) { AgentParameters _param = NetworkHelper.GetSNMPV2Param(NetworkHelper.SNMP_COMMUNITY_STRING); _expectedIfNr = ParameterCountRequest(_param, _target); if (_expectedIfNr == 0) { return("<color><red>ERROR: there are no Devices"); } PreparePackets(); string _output = SendRequests(_target, _param); return(_output); } else if (version == 3) { SecureAgentParameters _param = NetworkHelper.GetSNMPV3Param(_target, NetworkHelper.SNMP_COMMUNITY_STRING, secModel); _expectedIfNr = ParameterCountRequest(_param, _target); if (_expectedIfNr == 0) { return("<color><red>ERROR: there are no Devices"); } PreparePackets(); string _output = SendRequests(_target, _param); return(_output); } else { Logger.Log("SNMP Request Error. Unsupported version of SNMP", Logger.MessageType.ERROR); return(_errorMessage + "This is not a supported version of snmp"); } }
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 void ExecuteRequest(UdpTarget target, SecureAgentParameters param) { string outOctetsString = "1.3.6.1.2.1.2.2.1.16"; Oid outOctets = new Oid(outOctetsString + "." + ifEntryNr); Pdu octets = new Pdu(PduType.Get); octets.VbList.Add(outOctets); try { SnmpV3Packet _octetsResult = (SnmpV3Packet)target.Request(octets, param); if (_octetsResult.ScopedPdu.ErrorStatus != 0) { outp.outState = JobOutput.OutState.Failed; Logging.Logger.Log("Something went wrong with reading the traffic. The Error Status of SNMP was not equal 0, but " + _octetsResult.ScopedPdu.ErrorStatus.ToString(), Logging.Logger.MessageType.ERROR); } else { uint _result = (uint)Convert.ToUInt64(_octetsResult.Pdu.VbList[0].Value.ToString()) - _lastRecord; _lastRecord = (uint)Convert.ToUInt64(_octetsResult.Pdu.VbList[0].Value.ToString()); if (firstRun) { outp.GetOutputDesc("Bytes").dataObject = 0; } else { outp.GetOutputDesc("Bytes").dataObject = _result; } outp.outState = JobOutput.OutState.Success; Logging.Logger.Log("Traffic Read reports Success", Logging.Logger.MessageType.DEBUG); } } catch (Exception ex) { outp.outState = JobOutput.OutState.Failed; Logging.Logger.Log("Something went wrong with reading the traffic. The message is: " + ex.Message, Logging.Logger.MessageType.ERROR); } }
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); }