PowerShell tips for sending TCP requests _powershell

Source: Internet
Author: User
Tags flush http request

Many times we need to send a specific TCP request via the socket to a specific port on the server to implement the service opened by the probe server's specified port. Many languages have a corresponding way to achieve these requirements, of course, PowerShell is no exception, such as we want to send a simple HTTP request to the specified Web server:
get/http/1.1
Host:cn.bing.com

Here we would like to request Microsoft Bing's Chinese home page, if you need to send a GET request to the cn.bing.com server via PowerShell, you need to create a System.Net.Sockets.TcpClient object to send the request to the specified server and port.

The specific code is as follows:

Copy Code code as follows:

===== FileName: send-tcprequest.ps1=====
########################################
# SEND-TCPREQUEST.PS1
# # Send A TCP request to a remote computer, and return the response.
# # IF You don't supply input to this script (via either the pipeline, or the
# #-inputobject parameter, the script operates in interactive mode.
##
# # Example:
##
# # $http = @ '
# # get/http/1.1
# # Host:cn.bing.com
# # ' n ' n
## "@
##
# # $http |. \send-tcprequest cn.bing.com 80
########################################
Param
[string] $remoteHost = "localhost",
[int] $port = 80,
[Switch] $UseSSL,
[String] $inputObject,
[int] $commandDelay = 100
)

[string] $output = ""

# # Store The input into an array so we can scan over. If There was no input,
# # Then we are in interactive mode.
$currentInput = $inputObject
if (-not $currentInput)
{
$SCRIPT: Currentinput = @ ($input)
}
$scriptedMode = [BOOL] $currentInput

function Main
{
# # Open The socket, and connect to the computer on the specified port
if (-not $scriptedMode)
{
Write-host "Connecting to $remoteHost on port $port"
}

Trap {WRITE-ERROR "could not connect to remote computer: $_"; exit}
$socket = New-object System.Net.Sockets.TcpClient ($remoteHost, $port)

if (-not $scriptedMode)
{
Write-host "Connected. Press ^d followed by [ENTER] to exit. ' N '
}

$stream = $socket. GetStream ()

if ($UseSSL)
{
$sslStream = New-object System.Net.Security.SslStream $stream, $false
$sslStream. AuthenticateAsClient ($remoteHost)
$stream = $sslStream
}

$writer = New-object System.IO.StreamWriter $stream

while ($true)
{
# # Receive The output that has buffered so far
$SCRIPT: output + + GetOutput

# # If we ' re in scripted mode, send the commands,
# # receive the output, and exit.
if ($scriptedMode)
{
foreach ($line in $currentInput)
{
$writer. WriteLine ($line)
$writer. Flush ()
Start-sleep-m $commandDelay
$SCRIPT: output + + GetOutput
}

Break
}
# # If we ' re in interactive mode, write the buffered
# # output, and respond to input.
Else
{
if ($output)
{
foreach ($line in $output. Split ("' N"))
{
Write-host $line
}
$SCRIPT: output = ""
}

# # Read The user ' s command, quitting if they hit ^d
$command = Read-host
if ($command-eq ([char] 4)) {break;}

# # Otherwise, Write their command to the remote host
$writer. WriteLine ($command)
$writer. Flush ()
}
}

# # Close the streams
$writer. Close ()
$stream. Close ()

# # If we ' re in scripted mode, return the output
if ($scriptedMode)
{
$output
}
}

# # Read output from a remote host
function GetOutput
{
# # Create A buffer to receive the response
$buffer = New-object system.byte[] 1024
$encoding = New-object System.Text.AsciiEncoding

$outputBuffer = ""
$foundMore = $false

# # Read All the ' data available from the ' stream, writing it to the
# # Output buffer when done.
Todo
{
# # Allow data to buffer for a bit
START-SLEEP-M 1000

# # Read What data is available
$foundmore = $false
$stream. ReadTimeout = 1000

Todo
{
Try
{
$read = $stream. Read ($buffer, 0, 1024)

if ($read-gt 0)
{
$foundmore = $true
$outputBuffer + = ($encoding. GetString ($buffer, 0, $read))
}
catch {$foundMore = $false; $read = 0}
while ($read-GT 0)
while ($foundmore)

$outputBuffer
}
. Main
The script uses the following methods:
$http = @ "

get/http/1.1
Host:cn.bing.com
' N ' n
"@
$http |. \send-tcprequest cn.bing.com 80

The execution effect is as shown in the figure:

It's important to note that because the page returns too long, at least the returned content is cached in a variable and only the first 10 rows of the variable are exported.
With this script, we can send a specific request to the specified Web server to simulate the login and operation functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.