{"id":33507,"date":"2013-10-02T17:51:57","date_gmt":"2013-10-02T16:51:57","guid":{"rendered":"http:\/\/www.fuhaijun.com\/?p=33507"},"modified":"2024-11-06T11:18:46","modified_gmt":"2024-11-06T03:18:46","slug":"%e9%80%9a%e8%bf%87powershell%e5%8f%91%e9%80%81tcp%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33507","title":{"rendered":"\u901a\u8fc7PowerShell\u53d1\u9001TCP\u8bf7\u6c42"},"content":{"rendered":"<p>\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u901a\u8fc7Socket\u53d1\u9001\u7279\u5b9a\u7684TCP\u8bf7\u6c42\u7ed9\u670d\u52a1\u5668\u7684\u7279\u5b9a\u7aef\u53e3\u6765\u5b9e\u73b0\u63a2\u6d4b\u670d\u52a1\u5668\u7684\u6307\u5b9a\u7aef\u53e3\u6240\u5f00\u542f\u7684\u670d\u52a1\u3002\u5f88\u591a\u8bed\u8a00\u90fd\u6709\u76f8\u5e94\u7684\u65b9\u6cd5\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff0c\u5f53\u7136\uff0cPowerShell\u4e5f\u4e0d\u4f8b\u5916\uff0c\u6bd4\u5982\u6211\u4eec\u8981\u53d1\u9001\u4e00\u4e2a\u7b80\u5355\u7684http\u8bf7\u6c42\u5230\u6307\u5b9a\u7684web\u670d\u52a1\u5668\uff1a<\/p>\n<p>GET \/ HTTP\/1.1    <br \/>Host:cn.bing.com <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u60f3\u8bf7\u6c42\u5fae\u8f6f\u5fc5\u5e94\u7684\u4e2d\u6587\u9996\u9875\uff0c\u5982\u679c\u9700\u8981\u901a\u8fc7PowerShell\u5411cn.bing.com\u670d\u52a1\u5668\u53d1\u9001get\u8bf7\u6c42\uff0c\u5c31\u9700\u8981\u521b\u5efa\u4e00\u4e2aSystem.Net.Sockets.TcpClient\u5bf9\u8c61\uff0c\u5411\u6307\u5b9a\u7684\u670d\u52a1\u5668\u548c\u7aef\u53e3\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre style=\"width: 100.99%; font: 9pt verdana, fixedsys, verdana, tahoma; height: 2333px\"><span class=\"line_num\">        =====\u6587\u4ef6\u540d\uff1aSend-TcpRequest.ps1=====<\/span>\n<font color=\"#008000\">######################################## <\/font>\n<font color=\"#008000\"># Send-TcpRequest.ps1 <\/font>\n<font color=\"#008000\">## Send a TCP request to a remote computer, and return the response. <\/font>\n<font color=\"#008000\">## If you do not supply input to this script (via either the pipeline, or the <\/font>\n<font color=\"#008000\">## -InputObject parameter,) the script operates in interactive mode. <\/font>\n<font color=\"#008000\">## <\/font>\n<font color=\"#008000\">## Example: <\/font>\n<font color=\"#008000\">## <\/font>\n<font color=\"#008000\">## $http = @&quot; <\/font>\n<font color=\"#008000\">## GET \/ HTTP\/1.1 <\/font>\n<font color=\"#008000\">## Host:cn.bing.com  <\/font>\n<font color=\"#008000\">## `n`n <\/font>\n<font color=\"#008000\">## &quot;@ <\/font>\n<font color=\"#008000\">## <\/font>\n<font color=\"#008000\">## $http | .\\Send-TcpRequest cn.bing.com  80 <\/font>\n<font color=\"#008000\">######################################## <\/font>\n<font color=\"#0000ff\">param<\/font>( \n        [<font color=\"#0000ff\">string<\/font>] $remoteHost = <font color=\"#ff00ff\">&quot;localhost&quot;<\/font>, \n        [<font color=\"#0000ff\">int<\/font>] $port = 80, \n        [<font color=\"#0000ff\">switch<\/font>] $UseSSL, \n        [<font color=\"#0000ff\">string<\/font>] $inputObject, \n        [<font color=\"#0000ff\">int<\/font>] $commandDelay = 100 \n     ) \n\n[<font color=\"#0000ff\">string<\/font>] $output = <font color=\"#ff00ff\">&quot;&quot;<\/font> \n\n<font color=\"#008000\">## Store the input into an array that we can scan over. If there was no input, <\/font>\n<font color=\"#008000\">## then we will be in interactive mode. <\/font>\n$currentInput = $inputObject \n<font color=\"#0000ff\">if<\/font>(<font color=\"#0000ff\">-not<\/font> $currentInput) \n{ \n    $SCRIPT:currentInput = @($input) \n} \n$scriptedMode = [bool] $currentInput \n\n<font color=\"#0000ff\">function<\/font> Main \n{ \n<font color=\"#008000\">    ## Open the socket, and connect to the computer on the specified port <\/font>\n    <font color=\"#0000ff\">if<\/font>(<font color=\"#0000ff\">-not<\/font> $scriptedMode) \n    { \n        <font color=\"#008080\">write-host<\/font> <font color=\"#ff00ff\">&quot;Connecting to $remoteHost on port $port&quot;<\/font> \n    } \n\n    <font color=\"#0000ff\">trap<\/font> { <font color=\"#008080\">Write-Error<\/font> <font color=\"#ff00ff\">&quot;Could not connect to remote computer: $_&quot;<\/font>; exit } \n    $socket = <font color=\"#008080\">new-object<\/font> System.Net.Sockets.TcpClient($remoteHost, $port) \n\n    <font color=\"#0000ff\">if<\/font>(<font color=\"#0000ff\">-not<\/font> $scriptedMode) \n    { \n        <font color=\"#008080\">write-host<\/font> <font color=\"#ff00ff\">&quot;Connected. Press ^D followed by [ENTER] to exit.`n&quot;<\/font> \n    } \n\n    $stream = $socket.GetStream() \n\n    <font color=\"#0000ff\">if<\/font>($UseSSL) \n    { \n        $sslStream = <font color=\"#008080\">New-Object<\/font> System.Net.Security.SslStream $stream,$false \n        $sslStream.AuthenticateAsClient($remoteHost) \n        $stream = $sslStream \n    } \n\n    $writer = <font color=\"#008080\">new-object<\/font> System.IO.StreamWriter $stream \n\n    <font color=\"#0000ff\">while<\/font>($true) \n    { \n<font color=\"#008000\">        ## Receive the output that has buffered so far <\/font>\n        $SCRIPT:output += GetOutput \n\n<font color=\"#008000\">        ## If we're in scripted mode, send the commands, <\/font>\n<font color=\"#008000\">        ## receive the output, and exit. <\/font>\n        <font color=\"#0000ff\">if<\/font>($scriptedMode) \n        { \n            <font color=\"#0000ff\">foreach<\/font>($line <font color=\"#0000ff\">in<\/font> $currentInput) \n            { \n                $writer.WriteLine($line) \n                $writer.Flush() \n                <font color=\"#008080\">Start-Sleep<\/font> -m $commandDelay \n                $SCRIPT:output += GetOutput \n            } \n\n            <font color=\"#0000ff\">break<\/font> \n        } \n<font color=\"#008000\">        ## If we're in interactive mode, write the buffered <\/font>\n<font color=\"#008000\">        ## output, and respond to input. <\/font>\n        <font color=\"#0000ff\">else<\/font> \n        { \n            <font color=\"#0000ff\">if<\/font>($output)  \n            { \n                <font color=\"#0000ff\">foreach<\/font>($line <font color=\"#0000ff\">in<\/font> $output.Split(<font color=\"#ff00ff\">&quot;`n&quot;<\/font>)) \n                { \n                    <font color=\"#008080\">write-host<\/font> $line \n                } \n                $SCRIPT:output = <font color=\"#ff00ff\">&quot;&quot;<\/font> \n            } \n\n<font color=\"#008000\">            ## Read the user's command, quitting if they hit ^D <\/font>\n            $command = <font color=\"#008080\">read-host<\/font> \n            <font color=\"#0000ff\">if<\/font>($command <font color=\"#0000ff\">-eq<\/font> ([char] 4)) { <font color=\"#0000ff\">break<\/font>; } \n\n<font color=\"#008000\">            ## Otherwise, Write their command to the remote host <\/font>\n            $writer.WriteLine($command) \n            $writer.Flush() \n        } \n    } \n\n<font color=\"#008000\">    ## Close the streams <\/font>\n    $writer.Close() \n    $stream.Close() \n\n<font color=\"#008000\">    ## If we're in scripted mode, return the output <\/font>\n    <font color=\"#0000ff\">if<\/font>($scriptedMode) \n    { \n        $output \n    } \n} \n\n<font color=\"#008000\">## Read output from a remote host <\/font>\n<font color=\"#0000ff\">function<\/font> GetOutput \n{ \n<font color=\"#008000\">    ## Create a buffer to receive the response <\/font>\n    $buffer = <font color=\"#008080\">new-object<\/font> System.Byte[] 1024 \n    $encoding = <font color=\"#008080\">new-object<\/font> System.Text.AsciiEncoding \n\n    $outputBuffer = <font color=\"#ff00ff\">&quot;&quot;<\/font> \n    $foundMore = $false \n\n<font color=\"#008000\">    ## Read all the data available from the stream, writing it to the <\/font>\n<font color=\"#008000\">    ## output buffer when done. <\/font>\n    <font color=\"#0000ff\">do<\/font> \n    { \n<font color=\"#008000\">        ## Allow data to buffer for a bit <\/font>\n        <font color=\"#008080\">start-sleep<\/font> -m 1000 \n\n<font color=\"#008000\">        ## Read what data is available <\/font>\n        $foundmore = $false \n        $stream.ReadTimeout = 1000 \n\n        <font color=\"#0000ff\">do<\/font> \n        { \n            try \n            { \n                $read = $stream.Read($buffer, 0, 1024) \n\n                <font color=\"#0000ff\">if<\/font>($read <font color=\"#0000ff\">-gt<\/font> 0) \n                { \n                    $foundmore = $true \n                    $outputBuffer += ($encoding.GetString($buffer, 0, $read)) \n                } \n            } catch { $foundMore = $false; $read = 0 } \n        } <font color=\"#0000ff\">while<\/font>($read <font color=\"#0000ff\">-gt<\/font> 0) \n    } <font color=\"#0000ff\">while<\/font>($foundmore) \n\n    $outputBuffer \n} \n. Main <\/pre>\n<p>\u8be5\u811a\u672c\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>$http = @&quot;<br \/>\n  <br \/>GET \/ HTTP\/1.1 <\/p>\n<p>Host:cn.bing.com <\/p>\n<p>`n`n <\/p>\n<p>&quot;@ <\/p>\n<p>$http | .\\Send-TcpRequest cn.bing.com 80<\/p>\n<p>\u6267\u884c\u6548\u679c\u5982\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2013\/10\/image2.png\" width=\"669\" height=\"438\" \/> <\/p>\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u7531\u4e8e\u9875\u9762\u8fd4\u56de\u7684\u5185\u5bb9\u592a\u957f\u4e86\uff0c\u8fd9\u91cc\u81f3\u5c11\u662f\u5c06\u8fd4\u56de\u7684\u5185\u5bb9\u7f13\u5b58\u5728\u4e00\u4e2a\u53d8\u91cf\u91cc\uff0c\u5e76\u53ea\u8f93\u51fa\u4e86\u53d8\u91cf\u7684\u593410\u884c\u3002<\/p>\n<p>\u6709\u4e86\u8fd9\u4e2a\u811a\u672c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5411\u6307\u5b9a\u7684web\u670d\u52a1\u5668\u53d1\u9001\u7279\u5b9a\u7684\u8bf7\u6c42\uff0c\u6765\u5b9e\u73b0\u6a21\u62df\u767b\u9646\u548c\u64cd\u4f5c\u7684\u529f\u80fd\u4e86\u3002<\/p>\n<p>&#160;<\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b<br \/>\n  <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709<\/p>\n<p>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22<\/p>\n<p>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\u4e14\u4fdd\u8bc1\u5185\u5bb9\u5b8c\u6574!\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb!<\/p>\n<p>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/www.fuhaijun.com\/\">http:\/\/www.fuhaijun.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u901a\u8fc7Socket\u53d1\u9001\u7279\u5b9a\u7684TCP\u8bf7\u6c42\u7ed9\u670d\u52a1\u5668\u7684\u7279\u5b9a\u7aef\u53e3\u6765\u5b9e\u73b0\u63a2\u6d4b\u670d\u52a1\u5668\u7684\u6307\u5b9a\u7aef\u53e3\u6240\u5f00\u542f\u7684\u670d\u52a1\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,1],"tags":[],"class_list":["post-33507","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33507"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33507"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33507\/revisions"}],"predecessor-version":[{"id":33739,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33507\/revisions\/33739"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}