Page 1 of 2

Powershell create multicast listener

Posted: Thu Nov 16, 2017 3:21 pm
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.

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 3:51 pm
by jvierra
Search for c# examples of how to open a UDP port as a listener. There are quite a few out there.

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 3:53 pm
by jvierra
Here is one that is fairly complete: http://www.jarloo.com/c-udp-multicasting-tutorial/

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 3:56 pm
by sekou2331
Are you saying there is no way of writing this in powershell?

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 4:14 pm
by jvierra
Convert the C# example to PowerShell.

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 4:18 pm
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.

:)

Re: Powershell create multicast listener

Posted: Thu Nov 16, 2017 4:52 pm
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);
    }
}

Re: Powershell create multicast listener

Posted: Mon Nov 20, 2017 10:01 am
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

Re: Powershell create multicast listener

Posted: Mon Nov 20, 2017 10:33 am
by jvierra
"new" only works in PS 5.

$client = New-Object System.Net.Sockets.UdpClient

Re: Powershell create multicast listener

Posted: Mon Nov 20, 2017 12:41 pm
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));
    }
}