Friday, May 15, 2015

(PowerShell) Quick and dirty NetCat style listener

I was trying to come up with something in PowerShell that would listen on an arbitrary port and return a string. This seems to do the job perfectly.


while ($true)
{
    $listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse('0.0.0.0'), 42000)

    $listener.start()

    $client = $listener.AcceptTcpClient()

    $stream = $client.GetStream()
   
    $stWriter = new-Object System.IO.StreamWriter($stream)
   
    $stWriter.WriteLine("Put your text right here")
    $stWriter.Flush
    $stWriter.Close()

    $client.Close()

    $listener.Stop()

    write-host "Connection closed."
}

No comments:

Post a Comment