Ich habe eine kleine Consolenanwendung geschrieben, die per Active Directory nach Rechnern im Netz sucht und guggt ob diese Online sind. Dann wird noch ein Packet hingesendet. Das ganze ist mit einem Timeout versehen, so dass die Suche nicht zu lange dauert.
Will euch das ganze nicht vorenthalten.
Gruß, Alf
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.Net;
using System.Net.Sockets;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
namespace adsClient
{
class Program
{
static Thread th;
static System.Timers.Timer t; //nicht System.Threading.Timer
static string hostName;
static IPAddress hostIp;
static byte[] b;
static IPEndPoint serverEp;
static IPEndPoint clientEp;
static UdpClient uClient;
static void Main(string[] args)
{
SearchResultCollection results;
DirectorySearcher search = new DirectorySearcher();
DirectoryEntry entry;
search.Filter = String.Format("(cn={0})", "Computers");
results = search.FindAll();
hostName = "";
hostIp = IPAddress.Any;
b = new Byte[1024];
serverEp = new IPEndPoint(IPAddress.Any, 55554);
clientEp = new IPEndPoint(IPAddress.Any, 55553);
uClient = new UdpClient(clientEp);
t = new System.Timers.Timer();
t.Interval = 153;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
foreach (SearchResult r in results)
{
entry = r.GetDirectoryEntry();
foreach (DirectoryEntry e in entry.Children)
{
hostName = e.Name.Substring(3);
try
{
th = new Thread(new ParameterizedThreadStart(GetIPThread));
th.Start(hostName);
}
catch (ThreadStartException ex)
{
Console.WriteLine(ex.Message);
}
t.Start();
while (th.ThreadState == System.Threading.ThreadState.AbortRequested) {}
t.Stop();
}
}
}
static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
t.Stop();
th.Abort();
}
static void GetIPThread(object hName)
{
try
{
IPHostEntry i = Dns.GetHostEntry((string)hName);
hostIp = i.AddressList[0];
serverEp = new IPEndPoint(hostIp, 55554);
Console.WriteLine((string)hName + ": \t" + hostIp);
b = Encoding.ASCII.GetBytes((string)hName);
uClient.Send(b, b.Length, serverEp);
}
catch (SocketException e)
{
Console.WriteLine((string)hName + " " + e.Message);
}
}
}
}
Allen die das nicht innerhalb eines Programms brauchen sei auch noch "net view" nahegelegt. (Prüft allerdings nicht ob die Rechner auch on sind)