使用C#通过网络检测SNMP设备 [英] Detect SNMP device over network with C#
问题描述
亲爱的我正在开发一个c#服务,该服务应扫描网络以检测活动的snmp设备。我希望得到一个类似于Linux运行的NMAP命令结果的结果,在设备设备方面。
nmap -P0 -v -sU -p 161 192.168.1.0/24
我的尝试:
为了测试,我做了一个小应用程序来扫描开放的UDP端口。首先,我在端口161上运行一个侦听器,然后运行发送器。但是我只能通过1台设备取回响应,而NMAP则给了我更多设备。
有什么建议吗?
先谢谢,
这里你是我的代码:
UDP listner
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
<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();
}
和这里 UDP发件人
and here UDP Sender
<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();
}
}
推荐答案
我在Google搜索中使用了您的问题标题:使用C检测网络上的SNMP设备 - Google搜索 [ ^ ]并找到: SnmpSharpNet |适用于C#的SNMP库 [ ^ ]
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#[^]
>
这篇关于使用C#通过网络检测SNMP设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!