protected override void Append(LoggingEvent loggingEvent) { string message = RenderLoggingEvent(loggingEvent); var vars = new List <Variable>(); vars.Add(new Variable(new Oid(OIDprefix), new OctetString(message))); TrapV2Message trap; lock (Msglock) { trap = new TrapV2Message( requestId++, VersionCode.V2, new OctetString(community), new Oid(enterprise), Convert.ToUInt32(DateTimeOffset.UtcNow.ToUnixTimeSeconds()), vars); } trap.Send(new IPEndPoint(IPAddress.Parse(serverip), safeParseInt(serverport, 162))); }
public static void Trap(ObjectIdentifier oid, ISnmpData data, params IPEndPoint[] eps) { ThreadPool.QueueUserWorkItem(delegate { try { List <Variable> vList = new List <Variable> { new Variable(oid, data) }; #if !DEBUG /** 20190123 Para seleccionar la IP SOURCE del TRAP */ using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { var endPointLocal = new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.SipIp), 0); sender.Bind(endPointLocal); foreach (IPEndPoint ep in eps) { TrapV2Message message = new TrapV2Message(0, VersionCode.V2, new OctetString("public"), new ObjectIdentifier(Settings.Default.BaseOid), 0, vList); message.Send(ep, sender); } } #else foreach (IPEndPoint ep in eps) { Messenger.SendTrapV2(0, VersionCode.V2, ep, new OctetString("public"), new ObjectIdentifier(Settings.Default.BaseOid), 0, vList); } #endif } catch (Exception /*e*/) { } }); }
public void TrapFromTo(string ipFrom, string ipTo, string community, string oid, string val, Int32 port = 162, VersionCode snmpVersion = VersionCode.V2) { using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { var variables = new List <Variable>() { new Variable(new ObjectIdentifier(oid), new OctetString(val)) }; var endPointLocal = new IPEndPoint(IPAddress.Parse(ipFrom), 0); var receiver = new IPEndPoint(IPAddress.Parse(ipTo), port); sender.Bind(endPointLocal); TrapV2Message message = new TrapV2Message(0, VersionCode.V2, new OctetString(community), new ObjectIdentifier(oid), 0, variables); message.Send(receiver, sender); } }
/// <summary> /// Invio effettivo TRAP /// </summary> /// <param name="id">Identificativo oggetto</param> /// <param name="variables">Elenco parametri da inviare</param> /// <returns></returns> public bool SendTrap(string id, List <Variable> variables) { bool success = false; try { switch (myVersion) { case "V1": // Lextm.SharpSnmpLib.VersionCode.V1: Messenger.SendTrapV1( new IPEndPoint(IpRequestManager, IpRequestPORT), IpRequestSender, UName, new ObjectIdentifier(id), GenericCode.ColdStart, 0, //specific 0, //timestamp variables ); break; case "V2": // Lextm.SharpSnmpLib.VersionCode.V2: Messenger.SendTrapV2( 0, //request Id Lextm.SharpSnmpLib.VersionCode.V2, new IPEndPoint(IpRequestManager, IpRequestPORT), UName, new ObjectIdentifier(id), 0, //timestamp variables ); //var trap = new TrapV2Message( // VersionCode.V3, // 1, // 1, // UName, // new ObjectIdentifier(id), // 0, // variables, // DefaultPrivacyProvider.DefaultPair, // 0x10000, // new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")), // 0, // 0); //trap.Send(new IPEndPoint(IpRequestManager, IpRequestPORT)); break; case "V3": // Lextm.SharpSnmpLib.VersionCode.V3: var trap = new TrapV2Message( Lextm.SharpSnmpLib.VersionCode.V3, 528732060, //messageId 1905687779, //request Id UName, new ObjectIdentifier(id), 0, //time variables, DefaultPrivacyProvider.DefaultPair, 0x10000, //MaxMessageSize new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")), //Engine id 0, //engine boots 0); //engine time trap.Send(new IPEndPoint(IpRequestManager, IpRequestPORT)); break; } success = true; } catch (Exception ex) { ErrorString = String.Format("Message: {0}\r\nSource: {1}\r\nStackTrace:{2}", ex.Message, ex.Source, ex.StackTrace); } return(success); }
public static void Main(string[] args) { IPAddress address = args.Length == 1 ? IPAddress.Parse(args[0]) : IPAddress.Loopback; Messenger.SendTrapV1( new IPEndPoint(address, 162), IPAddress.Loopback, new OctetString("public"), new ObjectIdentifier(new uint[] { 1, 3, 6 }), GenericCode.ColdStart, 0, 0, new List <Variable>()); //Thread.Sleep(50); Messenger.SendTrapV2( 0, VersionCode.V2, new IPEndPoint(address, 162), new OctetString("public"), new ObjectIdentifier(new uint[] { 1, 3, 6 }), 0, new List <Variable>()); //Thread.Sleep(50); try { Messenger.SendInform( 0, VersionCode.V2, new IPEndPoint(address, 162), new OctetString("public"), new ObjectIdentifier(new uint[] { 1, 3, 6 }), 0, new List <Variable>(), 2000, null, null); } catch (Exception ex) { Console.WriteLine(ex); } try { Discovery discovery = Messenger.GetNextDiscovery(SnmpType.InformRequestPdu); ReportMessage report = discovery.GetResponse(2000, new IPEndPoint(address, 162)); Messenger.SendInform( 0, VersionCode.V3, new IPEndPoint(address, 162), new OctetString("neither"), new ObjectIdentifier(new uint[] { 1, 3, 6 }), 0, new List <Variable>(), 2000, DefaultPrivacyProvider.DefaultPair, report); } catch (Exception ex) { Console.WriteLine(ex); } { var trap = new TrapV2Message( VersionCode.V3, 528732060, 1905687779, new OctetString("neither"), new ObjectIdentifier("1.3.6"), 0, new List <Variable>(), DefaultPrivacyProvider.DefaultPair, 0x10000, new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")), 0, 0); trap.Send(new IPEndPoint(address, 162)); } { #if NET452 var trap = new TrapV2Message( VersionCode.V3, 528732060, 1905687779, new OctetString("privacy"), new ObjectIdentifier("1.3.6"), 0, new List <Variable>(), new DESPrivacyProvider( new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))), 0x10000, new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")), 0, 0); trap.Send(new IPEndPoint(address, 162)); #endif } }