7 Replies - 1745 Views - Last Post: 12 September 2012 - 09:32 AM Rate Topic: -----

#1 flexin1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-September 12

snmp trap receiver in c#

Posted 11 September 2012 - 12:25 PM

Hi Guys,

I am very new to coding and have been given a project at work to build a syslog and SNMP trap receiver i have got the syslog working ok but am stuck on the trap portion of the program. I can receive the snmp trap using a simple udp listener and have the data in a byte array but when using ASCII or unicode the data is garbled. i understand that SNMP is encoded in ASN.1 but am unable to figure out how to decode this.

Below is what i have i need to get the data out of the array "Data" into a string so that i can add this to both the email and the log file. This would replace the string Data2.

any help you guys could give would be great even if it's just an easier way to write what i have done so far, as i say am very new to coding so am probably doing things the hard way LOL. Also if you could explain the changes i would really like to understand this.

thanks in advance.

 
 UdpClient SNMP = new UdpClient(162);
            IPEndPoint EP = null;
            bool done = true;
            while (done)
            {
                Console.WriteLine("Waiting for Broadcast");
                byte[] Data = SNMP.Receive(ref EP);
                string Data2 = Encoding.ASCII.GetString(Data);
                string subject = "SNMP Condition Matched" + Data2;
                Console.WriteLine(DateTime.Now.ToString() + "Recivied SNMP Trap" + Data2);

                using (StreamWriter D = new StreamWriter("C:\\syslog\\syslog.txt"))
                {
                    D.WriteLine(DateTime.Now.ToString() + "Recivied SNMP Trap" + Data2);
                }

                ArrayList Config = new ArrayList();
                using (StreamReader E = new StreamReader("C:\\syslog\\config.ini"))
                { 
                    while (!E.EndOfStream)
                    {    
                       string T = E.ReadLine();
                       Config.Add(T);          
                    }
                    E.Close();
                }

                if (!Config.Contains(Data2))
                {
                    Email(subject, Data2);
                }




Is This A Good Question/Topic? 0
  • +

Replies To: snmp trap receiver in c#

#2 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: snmp trap receiver in c#

Posted 11 September 2012 - 12:35 PM

Don't re-invent from scratch...
http://sourceforge.n...s/snmpsharpnet/

Grab the library, add it to your solution like any other .net component and get on with your day. When you have free time you can download and study the source code as a learning resource.

There are tons of free .NET SNMP libraries already written and freely available. Just google "SNMP .NET"
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,409
  • Joined: 08-June 10

Re: snmp trap receiver in c#

Posted 11 September 2012 - 12:43 PM

This might help some:

http://sourceforge.n...s/snmpsharpnet/

Also, don't use ArrayList unless you're on .NET 1.1. ArrayLists were used before C# had generics. Now that we do, use List<T>, and replace T with the type you want to use. Then it'll be a typed list, instead of a list of Objects, like ArrayLists.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: snmp trap receiver in c#

Posted 11 September 2012 - 12:46 PM

LOL - You're getting slow Curtis. Old age setting in?
Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,409
  • Joined: 08-June 10

Re: snmp trap receiver in c#

Posted 11 September 2012 - 02:26 PM

Ha, sure is. Although come to think of it, I believe you're older than me...
Was This Post Helpful? 0
  • +
  • -

#6 flexin1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-September 12

Re: snmp trap receiver in c#

Posted 12 September 2012 - 02:37 AM

Thanks guys,

Ok i have added the dll to the project but as this is the first time using this i would appreciate some direction pointers on this.

do i still use a udp client to get the data or does this library use it's own methods ? would appreciate a leg up on this.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: snmp trap receiver in c#

Posted 12 September 2012 - 06:51 AM

The souceforge page for this library lists the home site for the project. There is very complete documentation, instructions, examples and so on

http://www.snmpsharpnet.com/

If you can't be bothered with reading the instructions and doing a little experimentation then you might reconsider your career choice. Software engineering is not a field that you will be hand-held through.
Was This Post Helpful? 0
  • +
  • -

#8 flexin1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-September 12

Re: snmp trap receiver in c#

Posted 12 September 2012 - 09:32 AM

sorry Guys,

my understanding of a forum is for knowledge sharing.

For the record i am am doing the reading of the project site and this isn't my career. I am trying as i say to learn and thought some pointers in the right direction might help as well as the reading would push me the right way i am not asking you to write the code for me as i never have.

I appreciated the advise you guys gave and have spent the morning reading the sites pages. I apologize if i have come across like i am unwilling to do the work but equally i'm not afraid to ask the question.

maybe i should have asked the question could you point me to documentation on snmp traps using snmp sharp.

Many thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1