PoshCode Logo PowerShell Code Repository

Send-TcpRequest.ps1 by Lee Holmes 17 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2218"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Send-TcpRequest
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Send a TCP request to a remote computer, and return the response.
  15. If you do not supply input to this script (via either the pipeline, or the
  16. -InputObject parameter,) the script operates in interactive mode.
  17.  
  18. .EXAMPLE
  19.  
  20. PS >$http = @"
  21.   GET / HTTP/1.1
  22.   Host:bing.com
  23.   `n`n
  24. "@
  25.  
  26. $http | Send-TcpRequest bing.com 80
  27.  
  28. #>
  29.  
  30. param(
  31.     ## The computer to connect to
  32.     [string] $ComputerName = "localhost",
  33.  
  34.     ## A switch to determine if you just want to test the connection
  35.     [switch] $Test,
  36.  
  37.     ## The port to use
  38.     [int] $Port = 80,
  39.  
  40.     ## A switch to determine if the connection should be made using SSL
  41.     [switch] $UseSSL,
  42.  
  43.     ## The input string to send to the remote host
  44.     [string] $InputObject,
  45.  
  46.     ## The delay, in milliseconds, to wait between commands
  47.     [int] $Delay = 100
  48. )
  49.  
  50. Set-StrictMode -Version Latest
  51.  
  52. [string] $SCRIPT:output = ""
  53.  
  54. ## Store the input into an array that we can scan over. If there was no input,
  55. ## then we will be in interactive mode.
  56. $currentInput = $inputObject
  57. if(-not $currentInput)
  58. {
  59.     $currentInput = @($input)
  60. }
  61. $scriptedMode = ([bool] $currentInput) -or $test
  62.  
  63. function Main
  64. {
  65.     ## Open the socket, and connect to the computer on the specified port
  66.     if(-not $scriptedMode)
  67.     {
  68.         write-host "Connecting to $computerName on port $port"
  69.     }
  70.  
  71.     try
  72.     {
  73.         $socket = New-Object Net.Sockets.TcpClient($computerName, $port)
  74.     }
  75.     catch
  76.     {
  77.         if($test) { $false }
  78.         else { Write-Error "Could not connect to remote computer: $_" }
  79.  
  80.         return
  81.     }
  82.  
  83.     ## If we're just testing the connection, we've made the connection
  84.     ## successfully, so just return $true
  85.     if($test) { $true; return }
  86.  
  87.     ## If this is interactive mode, supply the prompt
  88.     if(-not $scriptedMode)
  89.     {
  90.         write-host "Connected.  Press ^D followed by [ENTER] to exit.`n"
  91.     }
  92.  
  93.     $stream = $socket.GetStream()
  94.  
  95.     ## If we wanted to use SSL, set up that portion of the connection
  96.     if($UseSSL)
  97.     {
  98.         $sslStream = New-Object System.Net.Security.SslStream $stream,$false
  99.         $sslStream.AuthenticateAsClient($computerName)
  100.         $stream = $sslStream
  101.     }
  102.  
  103.     $writer = new-object System.IO.StreamWriter $stream
  104.  
  105.     while($true)
  106.     {
  107.         ## Receive the output that has buffered so far
  108.         $SCRIPT:output += GetOutput
  109.  
  110.         ## If we're in scripted mode, send the commands,
  111.         ## receive the output, and exit.
  112.         if($scriptedMode)
  113.         {
  114.             foreach($line in $currentInput)
  115.             {
  116.                 $writer.WriteLine($line)
  117.                 $writer.Flush()
  118.                 Start-Sleep -m $Delay
  119.                 $SCRIPT:output += GetOutput
  120.             }
  121.  
  122.             break
  123.         }
  124.         ## If we're in interactive mode, write the buffered
  125.         ## output, and respond to input.
  126.         else
  127.         {
  128.             if($output)
  129.             {
  130.                 foreach($line in $output.Split("`n"))
  131.                 {
  132.                     write-host $line
  133.                 }
  134.                 $SCRIPT:output = ""
  135.             }
  136.  
  137.             ## Read the user's command, quitting if they hit ^D
  138.             $command = read-host
  139.             if($command -eq ([char] 4)) { break; }
  140.  
  141.             ## Otherwise, Write their command to the remote host
  142.             $writer.WriteLine($command)
  143.             $writer.Flush()
  144.         }
  145.     }
  146.  
  147.     ## Close the streams
  148.     $writer.Close()
  149.     $stream.Close()
  150.  
  151.     ## If we're in scripted mode, return the output
  152.     if($scriptedMode)
  153.     {
  154.         $output
  155.     }
  156. }
  157.  
  158. ## Read output from a remote host
  159. function GetOutput
  160. {
  161.     ## Create a buffer to receive the response
  162.     $buffer = new-object System.Byte[] 1024
  163.     $encoding = new-object System.Text.AsciiEncoding
  164.  
  165.     $outputBuffer = ""
  166.     $foundMore = $false
  167.  
  168.     ## Read all the data available from the stream, writing it to the
  169.     ## output buffer when done.
  170.     do
  171.     {
  172.         ## Allow data to buffer for a bit
  173.         start-sleep -m 1000
  174.  
  175.         ## Read what data is available
  176.         $foundmore = $false
  177.         $stream.ReadTimeout = 1000
  178.  
  179.         do
  180.         {
  181.             try
  182.             {
  183.                 $read = $stream.Read($buffer, 0, 1024)
  184.  
  185.                 if($read -gt 0)
  186.                 {
  187.                     $foundmore = $true
  188.                     $outputBuffer += ($encoding.GetString($buffer, 0, $read))
  189.                 }
  190.             } catch { $foundMore = $false; $read = 0 }
  191.         } while($read -gt 0)
  192.     } while($foundmore)
  193.  
  194.     $outputBuffer
  195. }
  196.  
  197. . Main

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me