In many cases, we need to use a Socket to send a specific TCP request to a specific port of the server to detect services enabled by a specified port of the server. Many languages have corresponding methods to meet the above requirements. Of course, PowerShell is no exception. For example, we want to send a simple http request to the specified web Server:
GET, HTTP, 1.1
Host: cn.bing.com
Here we want to request the Chinese homepage of Microsoft Bing. To send a get request to the cn.bing.com server through PowerShell, we need to create a System. net. sockets. the TcpClient object that sends a request to the specified server and port.
The Code is as follows:
===== File Name: send-TcpRequest.ps1 ==== ################################## ###### Send-TcpRequest.ps1 ## Send a TCP request to a remote computer, and return the response. # If you do not 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' # "#####$ 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 that we can scan over. if there was no input, # then we will be 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 "cocould 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. do {## Allow data to buffer for a bit start-sleep-m 1000 ## Read what data is available $ foundmore = $ false $ stream. readTimeout = 1000 do {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 is used as follows:
$ Http = @"
GET, HTTP, 1.1
Host: cn.bing.com
'N'
"@
$ Http |. \ Send-TcpRequest cn.bing.com 80
Execution result:
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/001PQH8-0.png "width =" 669 "height =" 438 "/>
It should be noted that because the content returned on the page is too long, at least the returned content is cached in a variable and only the first 10 rows of the variable are output.
With this script, we can send specific requests to the specified web server to simulate login and operation.
Author: Fu haijun
Source: http://fuhj02.blog.51cto.com
Copyright: The copyright of this article is owned by the author and 51cto.
Reprinted: you are welcome to reprinted. Please Reprinted as required to save the author's Creative Enthusiasm.] Thank you.
Requirement: This statement must be retained without the author's consent. The original Article must be connected and the content must be complete! Otherwise, legal liability is required!
Personal Website: http://www.fuhaijun.com/