Skip to content

Instantly share code, notes, and snippets.

@rwinscot
Created November 26, 2018 20:45
Show Gist options
  • Save rwinscot/463b51f0da7e02ae46128fef80d000f2 to your computer and use it in GitHub Desktop.
Save rwinscot/463b51f0da7e02ae46128fef80d000f2 to your computer and use it in GitHub Desktop.
UNMS Ubiquiti Device Discovery
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace UbiquityUtils
{
class Program
{
private static void UnmsDeviceDiscovery()
{
Task.Run(async () =>
{
// Network segment where Ubiquiti devices can be found... via UDP announcement.
string ip = "10.10.10.255";
using (var udpClient = new UdpClient())
{
// Discovery packet is sent using UDP port 10001; devices respond only to packets
// sent from private network ranges.
// https://help.ubnt.com/hc/en-us/articles/115015690147-UNMS-Device-Discovery
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(ip), 10001);
Byte[] sendBytes = { 0x1, 0x0, 0x0, 0x0 };
udpClient.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);
string msg = "";
while (true)
{
// INVESTIGATE: UDP packets may not get a solid response - will timeouts be
// a concern?
UdpReceiveResult result = await udpClient.ReceiveAsync();
msg += Encoding.ASCII.GetString(result.Buffer);
// Device detail will be present in the result.
Console.WriteLine(result.RemoteEndPoint.AddressFamily);
Console.WriteLine(result.RemoteEndPoint.Address);
Console.WriteLine(msg);
}
}
});
}
static void Main(string[] args)
{
ConsoleKeyInfo cki;
do
{
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
switch (cki.KeyChar)
{
// Press 'S' to send a UNMS Device Discovery request.
case 's':
UnmsDeviceDiscovery();
break;
}
}
Thread.Sleep(10);
} while (true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment