-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkCollect.cs
152 lines (130 loc) · 4.6 KB
/
NetworkCollect.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Microsoft.SqlServer.Server;
using System;
using System.Data.SqlTypes;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace MySQLCLRFunctions
{
public static class NetworkCollect
{
[SqlFunction()]
public static SqlString PingGetAddress(SqlString Machine)
{
string host = Machine.ToString();
if (Machine.ToString().Contains("\\"))
{
host = Machine.ToString().Substring(0, Machine.ToString().IndexOf("\\"));
// Named instance
}
PingReply pr = PingHostGetReply(host);
if (pr.Status == IPStatus.Success)
return new SqlString(pr.Address.ToString());
else
return SqlString.Null;
}
[SqlFunction()]
public static SqlString GetMyIP4()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return SqlString.Null;
}
[SqlFunction()]
public static SqlBytes PingGetReturnBuffer(SqlString Machine)
{
string host = Machine.ToString();
if (Machine.ToString().Contains("\\"))
{
host = Machine.ToString().Substring(0, Machine.ToString().IndexOf("\\"));
// Named instance
}
PingReply pr = PingHostGetReply(host);
if (pr.Status == IPStatus.Success)
return new SqlBytes(pr.Buffer);
else
return SqlBytes.Null;
}
[SqlFunction()]
public static SqlString GetHostNames(SqlString MachineOrAlias)
{
string host = MachineOrAlias.ToString();
if (MachineOrAlias.ToString().Contains("\\"))
{
host = MachineOrAlias.ToString().Substring(0, MachineOrAlias.ToString().IndexOf("\\"));
// Named instance
}
IPHostEntry truehost = Dns.GetHostEntry(host);
string hosts = string.Join<IPAddress>(";", truehost.AddressList);
return new SqlString(hosts);
}
[SqlFunction()]
public static SqlString GetHostAliases(SqlString MachineOrAlias)
{
string host = MachineOrAlias.ToString();
if (MachineOrAlias.ToString().Contains("\\"))
{
host = MachineOrAlias.ToString().Substring(0, MachineOrAlias.ToString().IndexOf("\\"));
// Named instance
}
#pragma warning disable CS0618 // Type or member is obsolete
IPHostEntry truehost = Dns.Resolve(host);
#pragma warning restore CS0618 // Type or member is obsolete
string hosts = string.Join<string>(";", truehost.Aliases);
return new SqlString(hosts);
}
[SqlFunction()]
public static SqlString GetHostRealName(SqlString MachineOrAlias)
{
string host = MachineOrAlias.ToString();
if (MachineOrAlias.ToString().Contains("\\"))
{
host = MachineOrAlias.ToString().Substring(0, MachineOrAlias.ToString().IndexOf("\\"));
//return new SqlString(host);
// Named instance
}
try
{
#pragma warning disable CS0618 // Type or member is obsolete
IPHostEntry truehost = Dns.Resolve(host);
#pragma warning restore CS0618 // Type or member is obsolete
host = truehost.HostName;
}
catch (SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
return SqlString.Null;
}
return new SqlString(host);
}
private static PingReply PingHostGetReply(string nameOrAddress)
{
bool pingable;
Ping pinger = null;
PingReply reply = null;
try
{
pinger = new Ping();
reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
pinger?.Dispose();
}
return reply;
}
}
}