{"id":33540,"date":"2014-10-09T18:04:47","date_gmt":"2014-10-09T17:04:47","guid":{"rendered":"http:\/\/www.fuhaijun.com\/?p=33540"},"modified":"2024-11-06T11:18:46","modified_gmt":"2024-11-06T03:18:46","slug":"%e9%80%9a%e8%bf%87powershell%e8%8e%b7%e5%8f%96tcp%e5%93%8d%e5%ba%94%e7%b1%bbtelnet","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33540","title":{"rendered":"\u901a\u8fc7PowerShell\u83b7\u53d6TCP\u54cd\u5e94(\u7c7bTelnet)"},"content":{"rendered":"<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e86\u68c0\u6d4b\u6307\u5b9a\u7684TCP\u7aef\u53e3\u662f\u5426\u5b58\u6d3b\uff0c\u6211\u4eec\u90fd\u662f\u901a\u8fc7telnet\u6307\u5b9a\u7684\u7aef\u53e3\u770b\u662f\u5426\u6709\u54cd\u5e94\u6765\u786e\u5b9a\uff0c\u7136\u800c\u9ed8\u8ba4\u60c5\u51b5\u4e0bwin8\u4ee5\u540e\u7684\u7cfb\u7edf\u9ed8\u8ba4\u662f\u4e0d\u5b89\u88c5telnet\u7684\u3002\u8bbe\u60f3\u4e00\u4e0b\u5982\u679c\u4f60\u9ed1\u8fdb\u4e86\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u4e0a\u9762\u6ca1\u88c5telnet\uff0c\u4f46\u662f\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6e17\u900f\u8fdb\u5185\u7f51\uff0c\u9700\u8981\u63a2\u6d4b\u5185\u90e8\u670d\u52a1\u5668\u7279\u5b9a\u7aef\u53e3\u662f\u5426\u6253\u5f00\uff0c\u540c\u65f6\u4f60\u8fd8\u4e0d\u613f\u610f\u5b89\u88c5telnet\uff0c\u62c5\u5fc3\u5f15\u8d77\u7ba1\u7406\u5458\u6ce8\u610f\u3002\u90a3\u4e48\u597d\u5427\uff0c\u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u6211\u7684\u8fd9\u4e2a\u811a\u672c\u3002\u7531\u4e8e\u5b83\u662f\u539f\u751f\u6001\u7684PowerShell\u8bed\u53e5\u5b8c\u6210\uff0c\u6728\u6709telnet\u4f60\u4e5f\u7167\u6837\u80fd\u68c0\u6d4bTCP\u7aef\u53e3\u7684\u60c5\u51b5\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u9996\u5148\u4e0a\u4ee3\u7801\uff0c\u540e\u9762\u8fdb\u884c\u8bb2\u89e3:<\/p>\n<pre style=\"font: 9pt verdana, fixedsys, verdana, tahoma\"><span class=\"line_num\">        =====\u6587\u4ef6\u540d\uff1aGet-TCPResponse.ps1=====<\/span>\n<font color=\"#0000ff\">Function<\/font> Get-TCPResponse {\n&lt;<font color=\"#008000\"># Author:fuhj(powershell#live.cn ,http:\/\/fuhaijun.com)  \n        .SYNOPSIS\n            Tests TCP port of remote or local system and returns a response header\n            if applicable\n        .DESCRIPTION\n            Tests TCP port of remote or local system and returns a response header\n            if applicable\n            If server has no default response, then Response property will be NULL\n        .PARAMETER Computername\n            Local or remote system to test connection\n        .PARAMETER Port\n            TCP Port to connect to\n        .PARAMETER TCPTimeout\n            Time until connection should abort\n        .EXAMPLE\n        Get-TCPResponse -Computername pop.126.com -Port 110\n\n        Computername : pop.126.com \n        Port         : 110\n        IsOpen       : True\n        Response     : +OK Welcome to coremail Mail Pop3 Server (126coms[75c606d72bf436dfbce6.....])\n\n        Description\n        -----------\n        Checks port 110 of an mail server and displays header response.\n    #&gt;<\/font>\n    [OutputType('<font color=\"#008000\">Net.TCPResponse')]<\/font>\n    [cmdletbinding()]\n    <font color=\"#0000ff\">Param<\/font> (\n        [parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]\n        [Alias('__Server','IPAddress','IP','domain')]\n        [<font color=\"#0000ff\">string<\/font>[]]$Computername = $env:Computername,\n        [<font color=\"#0000ff\">int<\/font>[]]$Port = 25,\n        [<font color=\"#0000ff\">int<\/font>]$TCPTimeout = 1000\n    )\n    <font color=\"#0000ff\">Process<\/font> {\n        <font color=\"#0000ff\">ForEach<\/font> ($Computer <font color=\"#0000ff\">in<\/font> $Computername) {\n            <font color=\"#0000ff\">ForEach<\/font> ($_port <font color=\"#0000ff\">in<\/font> $Port) {\n                $stringBuilder = <font color=\"#008080\">New-Object<\/font> Text.StringBuilder\n                $tcpClient = <font color=\"#008080\">New-Object<\/font> System.Net.Sockets.TCPClient\n                $connect = $tcpClient.BeginConnect($Computer,$_port,$null,$null) \n                $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false) \n                <font color=\"#0000ff\">If<\/font> (<font color=\"#0000ff\">-NOT<\/font> $wait) {\n                    $object = [pscustomobject] @{\n                        Computername = $Computer\n                        Port = $_Port\n                        IsOpen = $False\n                        Response = $Null\n                    }\n                } <font color=\"#0000ff\">Else<\/font> {\n                    <font color=\"#0000ff\">While<\/font> ($True) {\n<font color=\"#008000\">                        #Let buffer<\/font>\n                        <font color=\"#008080\">Start-Sleep<\/font> -Milliseconds 1000\n                        <font color=\"#008080\">Write-Verbose<\/font> <font color=\"#ff00ff\">&quot;Bytes available: $($tcpClient.Available)&quot;<\/font>\n                        <font color=\"#0000ff\">If<\/font> ([int64]$tcpClient.Available <font color=\"#0000ff\">-gt<\/font> 0) {\n                            $stream = $TcpClient.GetStream()\n                            $bindResponseBuffer = <font color=\"#008080\">New-Object<\/font> Byte[] -ArgumentList $tcpClient.Available\n                            [<font color=\"#0000ff\">Int<\/font>]$response = $stream.Read($bindResponseBuffer, 0, $bindResponseBuffer.count)  \n                            $Null = $stringBuilder.Append(($bindResponseBuffer | <font color=\"#0000ff\">ForEach<\/font> {[char][<font color=\"#0000ff\">int<\/font>]$_}) -join <font color=\"#ff00ff\">''<\/font>)\n                        } <font color=\"#0000ff\">Else<\/font> {\n                            <font color=\"#0000ff\">Break<\/font>\n                        }\n                    } \n                    $object = [pscustomobject] @{\n                        Computername = $Computer\n                        Port = $_Port\n                        IsOpen = $True\n                        Response = $stringBuilder.Tostring()\n                    }\n                }\n                $object.pstypenames.insert(0,'<font color=\"#008000\">Net.TCPResponse')<\/font>\n                <font color=\"#008080\">Write-Output<\/font> $object\n                <font color=\"#0000ff\">If<\/font> ($Stream) {\n                    $stream.Close()\n                    $stream.Dispose()\n                }\n                $tcpClient.Close()\n                $tcpClient.Dispose()\n            }\n        }\n    }\n}<\/pre>\n<pre style=\"font: 9pt verdana, fixedsys, verdana, tahoma\">\n\u9996\u5148\u521b\u5efa\u4e00\u4e2aSystem.Net.Sockets.TCPClient\u5bf9\u8c61,\u53bb\u8fde\u63a5\u6307\u5b9a\u7684\u57df\u540d\u548c\u7aef\u53e3\uff0c\u77ac\u95f4\u65ad\u5f00\u7684\u90a3\u662f\u670d\u52a1\u5668\u6ca1\u5f00\u90a3\u4e2a\u7aef\u53e3\uff0c\u76f4\u63a5\u88ab\u62d2\u7edd\u4e86\uff0c\u5982\u679c\u6ca1\u62d2\u7edd\uff0c\u90a3\u5c31\u7b49\u7740\u670d\u52a1\u5668\u7aef\u7ed9\u4f60\u54cd\u5e94\uff0c\u7136\u540e\u8bfb\u53d6\u5b57\u8282\u6d41\u62fc\u63a5\u8d77\u6765\u8fdb\u884c\u89e3\u6790\u3002<\/pre>\n<pre style=\"font: 9pt verdana, fixedsys, verdana, tahoma\">\u6700\u540e\u9700\u8981\u5f3a\u8c03\u7684\u662f\u9700\u8981\u5bf9\u6253\u5f00\u7684\u6d41\u548cTCP\u8fde\u63a5\u8fdb\u884c\u5173\u95ed\uff0c\u4ee5\u4fbf\u91ca\u653e\u8d44\u6e90<\/pre>\n<p>\u8c03\u7528\u65b9\u6cd5\u5982\u4e0b:<\/p>\n<p>Get-TCPResponse -Computername pop.126.com -Port 110<\/p>\n<p>&#160;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2014\/10\/image4.png\" width=\"883\" height=\"207\" \/> <\/p>\n<p>\u518d\u5bf9\u6bd4\u4e00\u4e0btelnet\u7684\u7ed3\u679c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2014\/10\/image5.png\" width=\"885\" height=\"162\" \/><\/p>\n<p>\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4ee5\u540e\u6ca1\u6709telnet\u4e5f\u96be\u4e0d\u4f4f\u5927\u5bb6\u4e86\uff0chave fun\uff01^_^<\/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\uff1b\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>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e86\u68c0\u6d4b\u6307\u5b9a\u7684TCP\u7aef\u53e3\u662f\u5426\u5b58\u6d3b\uff0c\u6211\u4eec\u90fd\u662f\u901a\u8fc7telnet\u6307\u5b9a\u7684\u7aef\u53e3\u770b\u662f\u5426\u6709\u54cd\u5e94\u6765\u786e\u5b9a\uff0c\u7136\u800c\u9ed8\u8ba4\u60c5 [&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-33540","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\/33540"}],"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=33540"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33540\/revisions"}],"predecessor-version":[{"id":33733,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33540\/revisions\/33733"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}