Powershell create multicast listener

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Powershell create multicast listener

Post by sekou2331 »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell create multicast listener

Post by jvierra »

Search for c# examples of how to open a UDP port as a listener. There are quite a few out there.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell create multicast listener

Post by jvierra »

Here is one that is fairly complete: http://www.jarloo.com/c-udp-multicasting-tutorial/
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Powershell create multicast listener

Post by sekou2331 »

Are you saying there is no way of writing this in powershell?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell create multicast listener

Post by jvierra »

Convert the C# example to PowerShell.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Powershell create multicast listener

Post by mxtrinidad »

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.

:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell create multicast listener

Post by jvierra »

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);
    }
}
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Powershell create multicast listener

Post by sekou2331 »

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

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell create multicast listener

Post by jvierra »

"new" only works in PS 5.

$client = New-Object System.Net.Sockets.UdpClient
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Powershell create multicast listener

Post by sekou2331 »

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));
    }
}
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked