-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkTools.cs
86 lines (81 loc) · 3.42 KB
/
NetworkTools.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Heijden.DNS;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace NIHT.Plugins.Base {
public class NetworkTools {
public static bool Ping(string ip) {
return Ping(ip, 4);
}
public static bool Ping(string ip, int menge) {
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "abcdefghijklmnopqrstuvwxyzabcdef";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;
int success = 0;
Console.WriteLine("Ping wird ausgeführt für " + ip + " mit 32 Bytes Daten:");
for (int i = 0; i < menge; i++) {
try {
PingReply reply = pingSender.Send(ip, timeout, buffer, options);
if (reply.Status == IPStatus.Success) {
//Console.WriteLine("Antwort von {0} Bytes = {1} Zeit {2}ms TTL = {3}", ip, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);
success++;
} //else
//Console.WriteLine("Zeitüberschreitung");
} catch {
}
}
if (success > 0) return true;
return false;
}
public static string DNSLookup(string hostNameOrAddress) {
try {
IPHostEntry hostEntry = Dns.GetHostEntry(hostNameOrAddress);
IPAddress[] ips = hostEntry.AddressList;
return string.Join("; ", ips.Select(m => m.ToString()));
} catch (System.Net.Sockets.SocketException) {
return "";
}
}
public static string DNSLookupCustom(string hostNameOrAddress, string DNSServer) {
Resolver resolver = new Resolver();
Stopwatch sw = new Stopwatch();
sw.Start();
Response response = resolver.Query(hostNameOrAddress, QType.A, QClass.IN);
sw.Stop();
if (response.Error != "" || (response.RecordsA.Length == 0 && response.RecordsAAAA.Length == 0)) {
Console.WriteLine(";; " + response.Error);
return "";
} else {
return response.RecordsA[0].ToString();
}
}
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject {
T foundChild = null;
System.Collections.IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children) {
if (child is DependencyObject) {
DependencyObject depChild = child as DependencyObject;
if (child is FrameworkElement frameworkElement && frameworkElement.Name.IsEqual(childName)) {
try {
foundChild = (T)child;
} catch {
}
} else
foundChild = FindChild<T>(depChild, childName);
if (foundChild != null) return foundChild;
}
}
return foundChild;
}
}
}