1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
using System;
using System.Net.NetworkInformation;
using System.DirectoryServices;
namespace Pinger
{
class Program
{
static void Ping(string host)
{
using (Ping p = new Ping())
{
try
{
//Eventhandler anmelden welcher ausgeführt werden soll, wenn ein Ping durch ist.
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
byte[] buffer = new byte[32];
int timeOut = 1000;
PingOptions op = new PingOptions();
//host als userStat Object übergeben
// |
// \/
p.SendAsync(host, timeOut, buffer, op, host);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
static void Main(string[] args)
{
string domain = "eureDomain";
System.DirectoryServices.DirectoryEntry root = new System.DirectoryServices.DirectoryEntry(string.Concat("LDAP://",domain));
DirectorySearcher searcher = new DirectorySearcher(root);
//filter zur suche angeben, in diesem Fall, alle Pc´s
searcher.Filter = "(objectCategory=computer)";
foreach (SearchResult res in searcher.FindAll())
{
//ausgabe ist normaler Weiße:
//''CN=computername''
//Wir wollen nur den computernamen also schneiden wir den ''CN='' teil einfach weg
Ping(res.GetDirectoryEntry().Name.Replace("CN=", ""));
}
Console.ReadLine();
}
static void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
try
{
//wir wandeln das UserState Objekt in einen string um welcher unseren host darstellt.
Console.WriteLine("Ping statuse for ''{0}'' is ''{1}''",e.UserState.ToString(),e.Reply.Status);
}
catch (Exception)
{
Console.WriteLine(IPStatus.Unknown);
}
}
}
}
|