-
Notifications
You must be signed in to change notification settings - Fork 0
/
Syslog.psm1
120 lines (103 loc) · 3.47 KB
/
Syslog.psm1
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
function Send-Syslog {
Param(
[Parameter(Mandatory=$true, ParameterSetName="IP Address", Position=0)]
[string]$IPAddress,
[Parameter(Mandatory=$true, ParameterSetName="IPv6 Address", Position=0)]
[string]$IPv6Address,
[Parameter(Mandatory=$true, Position=1)]
[string]$Message,
[Parameter(Mandatory=$false, Position=2)]
[int]$Port = 514
)
$af = -1;
if ($IPAddress) {
$af = [System.Net.Sockets.AddressFamily]::InterNetwork;
$addr = $IPAddress;
} else {
$af = [System.Net.Sockets.AddressFamily]::InterNetworkV6;
$addr = $IPv6Address;
}
$ep = New-Object IPEndPoint ([IPAddress]::Parse($addr), $Port);
try
{
$socket = [System.Net.Sockets.UdpClient]::new($af);
$socket.Connect($ep);
$bytes = [System.Text.Encoding]::ASCII.GetBytes($message);
$i = $socket.Send($bytes, $bytes.length);
}
catch [System.Net.Sockets.SocketException]
{
Write-Error "Unable to send Syslog message to $addr";
}
finally
{
$socket.Close();
}
}
function Receive-Syslog {
Param(
[Parameter(Mandatory=$true, ParameterSetName="IP Address", Position=0)]
[string]$IPAddress,
[Parameter(Mandatory=$true, ParameterSetName="IPv6 Address", Position=0)]
[string]$IPv6Address,
[Parameter(Mandatory=$false, Position=1)]
[int]$Port = 514,
[Parameter(Mandatory=$false)]
[switch]$Listen = $false,
[Parameter(Mandatory=$false)]
[int]$Timeout = 0
)
# .NET does not automatically infer address family from the address.
# It assumes you are using IPv4 when you try to join a group, but if you
# give it an IPv6 address then you'll get an error. This is why I explicitly
# specify the address family when instantiating the UdpClient object.
if ($IPAddress) {
$addr = [IPAddress]::Parse($IPAddress);
$af = [System.Net.Sockets.AddressFamily]::InterNetwork;
} else {
$addr = [IPAddress]::Parse($IPv6Address);
$af = [System.Net.Sockets.AddressFamily]::InterNetworkV6;
}
if ($Listen -and $Timeout -eq 0) {
# CTRL+C does not work when listening for input.
# If the user specified to listen but did not give a timeout then
# we set it to 100 milliseconds.
# Exceptions from the timeout will be ignored in the inner catch statement below.
$Timeout = 100;
}
$ep = New-Object IPEndPoint ([IPAddress]::Any, 0);
try
{
$socket = New-Object System.Net.Sockets.UdpClient @($Port, $af);
$socket.JoinMulticastGroup($addr);
$socket.Client.ReceiveTimeout = $Timeout;
do
{
try
{
$bytes = $socket.Receive([ref] $ep);
[System.Text.Encoding]::ASCII.GetString($bytes);
}
catch [System.Net.Sockets.SocketException]
{
# suppress timeout errors
if ($Listen -eq $false)
{
Write-Error "Unable to receive Syslog messages on $addr";
}
}
}
while ($Listen);
}
catch [System.Net.Sockets.SocketException]
{
# this is an error we care about
Write-Error "Unable to receive Syslog messages on $addr";
}
finally
{
$socket.Close();
}
}
Set-Alias -Name txs -Value Send-Syslog
Set-Alias -Name rxs -Value Receive-Syslog