Powershell create multicast listener
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Powershell create multicast listener
I am looking for a way to test various multicast groups on windows server. Is there a way to write a powershell script to bind to a specific interface and attempt to consume data from a udp source? Right now I am using something called mdump.exe but it is not working and I am looking to write an alternative so that I can have more control.
Re: Powershell create multicast listener
Here is one that is fairly complete: http://www.jarloo.com/c-udp-multicasting-tutorial/
Re: Powershell create multicast listener
Are you saying there is no way of writing this in powershell?
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Powershell create multicast listener
Yes! You can write it with PowerShell, as it's a .NET technology, you can consume .NET code snippet as JVierra show you in the link.
Please read the Get-Help on Add-Type which show you how to do this.
Get-Help Add-Type -showwindow
This is one way to extend PowerShell using .NET Framework components and use by many in the community.
You can be very creative.
Please read the Get-Help on Add-Type which show you how to do this.
Get-Help Add-Type -showwindow
This is one way to extend PowerShell using .NET Framework components and use by many in the community.
You can be very creative.
Re: Powershell create multicast listener
Here is a quick linear conversion:
Code: Select all
function StartListener{
Param(
[Parameter(Mandatory)]
$IPAddress,
[Parameter(Mandatory)]
$Port
)
$client = [System.Net.Sockets.UdpClient]::New()
$client.ExclusiveAddressUse = $false;
$localEp = [System.Net.IPEndPoint]::New([IPAddress]::Any, $Port);
$client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true);
$client.ExclusiveAddressUse = $false;
$client.Client.Bind($localEp);
$multicastaddress = [IPAddress]::Parse($IpAddress);
$client.JoinMulticastGroup($multicastaddress);
[Console]::WriteLine('Listening this will never quit so you will need to Ctrl-Break it');
while ($true) {
[Byte[]]$data = $client.Receive([ref]$localEp);
$strData = [Encoding.Unicode]::GetString($data);
[Console]::WriteLine($strData);
}
}
Re: Powershell create multicast listener
Looking at this I am getting the error below. Also was looking into this and I wanted to know how would i force a interface. I see the MulticastOption looks to be the way to have a specific interface subscribe to a specific multicast group
Powershell version:
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
Method invocation failed because [System.Net.Sockets.UdpClient] does not contain a method named 'New'.
+ $client = [System.Net.Sockets.UdpClient]::New()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
Powershell version:
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
Re: Powershell create multicast listener
Ok so I changed the code to work with Powershell version 4. Is it suppose to outputing gibberish? I think the encoding is incorrect.
Code: Select all
function StartListener{
Param(
[Parameter(Mandatory)]
$IPAddress,
[Parameter(Mandatory)]
$Port
)
$client = New-Object System.Net.Sockets.UdpClient
$client.ExclusiveAddressUse = $false;
$localEp = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any,$port)
$client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true);
$client.ExclusiveAddressUse = $false;
$client.Client.Bind($localEp);
$multicastaddress = [IPAddress]::Parse($IpAddress);
$client.JoinMulticastGroup($multicastaddress);
[Console]::WriteLine('Listening this will never quit so you will need to Ctrl-Break it');
while ($true) {
[Byte[]]$data = $client.Receive([ref]$localEp);
$strData = New-object System.Text.UnicodeEncoding
$strData.GetString($data)
[Console]::WriteLine($strData.GetString($data));
}
}