Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear I'm developing a c# service which should scan network to detect active snmp devices. I would like to get a result similar to NMAP command results run with Linux, in term of devices founded.

nmap -P0 -v -sU -p 161 192.168.1.0/24

What I have tried:

Just for test, I made a small application to scan open UDP port. First I run a listener into a thread on port 161 and later i run sender. but i was able to get back the response only by 1 device, while NMAP give me back much more devices.

Any suggest?

Thanks in advance,

here you are my code:

UDP listner

C#
<pre>// UDP Listener
private void button1_Click(object sender, EventArgs e)
    {
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            int port = int.Parse(txtPort.Text);
            bool done = false;
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 161);
            UdpClient listener = new UdpClient(groupEP);
            string received_data;
            byte[] receive_byte_array;
            try
            {
                while (!done)
                {
                    Console.WriteLine("SERVER: Waiting for broadcast");
                    receive_byte_array = listener.Receive(ref groupEP);

                    if (!hosts.ContainsKey(groupEP.Address.ToString()) )   {
                        // Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
                        hosts.Add(groupEP.Address.ToString(), groupEP.Address.ToString());
                        Console.WriteLine("SERVER: Received a broadcast from {0}", groupEP.Address.ToString());
                        received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
                        //Console.WriteLine("data follows \n{0}\n\n", received_data);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }).Start();
    }


and here UDP Sender

C#
<pre>// UDP Sender
    private void button2_Click(object sender, EventArgs e)
    {
        new Thread(() =>
        {
            int port = int.Parse(txtPort.Text);
            string message = txtMessage.Text;
            Boolean done = false;
            Boolean exception_thrown = false;
            Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress send_to_address = IPAddress.Parse("192.168.1.255");
            IPEndPoint sending_end_point = new IPEndPoint(send_to_address, port);
            while (!done)
            {
                string text_to_send = message;
                if (text_to_send.Length == 0)
                {
                    done = true;
                }
                else
                {
                    byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
                    Console.WriteLine("CLIENT: sending to address: {0} port: {1}",
                    sending_end_point.Address,
                    sending_end_point.Port);
                    try
                    {
                        sending_socket.SendTimeout = 5000;
                        sending_socket.SendTo(send_buffer, sending_end_point);
                    }
                    catch (Exception send_exception)
                    {
                        exception_thrown = true;
                        Console.WriteLine("CLIENT:  Exception {0}", send_exception.Message);
                    }
                    if (exception_thrown == false)
                    {
                        Console.WriteLine("CLIENT: Message has been sent to the broadcast address");
                    }
                    else
                    {
                        exception_thrown = false;
                        Console.WriteLine("CLIENT: The exception indicates the message was not sent.");
                    }
                }
            } // end of while (!done)

        }).Start();
    }
}
Posted
Updated 28-Feb-17 9:01am

1 solution

I used your Question title in Google Search: Detect SNMP device over network with C - Google Search[^] and found this: SnmpSharpNet | SNMP Library for C#[^]
 
Share this answer
 
Comments
nemo14 1-Mar-17 4:54am    
thanks for your hint. The link is useful to query the device but i need before to identify the list of devices which have snmp port open (it uses UDP protocol). Any idea?
Graeme_Grant 1-Mar-17 5:08am    
I thought that was there when I read it. Have another look. Otherwise wouldn't talk long to google the answer like I did - see my google search link provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900