主頁 > 知識(shí)庫 > PowerShell小技巧之獲取TCP響應(yīng)(類Telnet)

PowerShell小技巧之獲取TCP響應(yīng)(類Telnet)

熱門標(biāo)簽:使用電話機(jī)器人電銷是否違法 信陽話務(wù)外呼系統(tǒng)怎么收費(fèi) 湖州電銷防封卡 電話智能外呼系統(tǒng)誠(chéng)信合作 什么渠道可以找外呼系統(tǒng)客戶 金融電銷公司怎么辦理外呼系統(tǒng) 安徽400電話辦理 雷霆電話機(jī)器人電話 真人和電話機(jī)器人對(duì)話

通常情況下,為了檢測(cè)指定的TCP端口是否存活,我們都是通過telnet指定的端口看是否有響應(yīng)來確定,然而默認(rèn)情況下win8以后的系統(tǒng)默認(rèn)是不安裝telnet的。設(shè)想一下如果你黑進(jìn)了一個(gè)服務(wù)器,上面沒裝telnet,但是為了進(jìn)一步滲透進(jìn)內(nèi)網(wǎng),需要探測(cè)內(nèi)部服務(wù)器特定端口是否打開,同時(shí)你還不愿意安裝telnet,擔(dān)心引起管理員注意。那么好吧,在這個(gè)情況下你需要我的這個(gè)腳本。由于它是原生態(tài)的PowerShell語句完成,木有telnet你也照樣能檢測(cè)TCP端口的情況了。

下面首先上代碼,后面進(jìn)行講解:

復(fù)制代碼 代碼如下:

        =====文件名:Get-TCPResponse.ps1=====
Function Get-TCPResponse {
# Author:fuhj(powershell#live.cn ,http://fuhaijun.com)
        .SYNOPSIS
            Tests TCP port of remote or local system and returns a response header
            if applicable
        .DESCRIPTION
            Tests TCP port of remote or local system and returns a response header
            if applicable
            If server has no default response, then Response property will be NULL
        .PARAMETER Computername
            Local or remote system to test connection
        .PARAMETER Port
            TCP Port to connect to
        .PARAMETER TCPTimeout
            Time until connection should abort
        .EXAMPLE
        Get-TCPResponse -Computername pop.126.com -Port 110

        Computername : pop.126.com
        Port         : 110
        IsOpen       : True
        Response     : +OK Welcome to coremail Mail Pop3 Server (126coms[75c606d72bf436dfbce6.....])

        Description
        -----------
        Checks port 110 of an mail server and displays header response.
    #>
    [OutputType('Net.TCPResponse')]
    [cmdletbinding()]
    Param (
        [parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [Alias('__Server','IPAddress','IP','domain')]
        [string[]]$Computername = $env:Computername,
        [int[]]$Port = 25,
        [int]$TCPTimeout = 1000
    )
    Process {
        ForEach ($Computer in $Computername) {
            ForEach ($_port in $Port) {
                $stringBuilder = New-Object Text.StringBuilder
                $tcpClient = New-Object System.Net.Sockets.TCPClient
                $connect = $tcpClient.BeginConnect($Computer,$_port,$null,$null)
                $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)
                If (-NOT $wait) {
                    $object = [pscustomobject] @{
                        Computername = $Computer
                        Port = $_Port
                        IsOpen = $False
                        Response = $Null
                    }
                } Else {
                    While ($True) {
                        #Let buffer
                        Start-Sleep -Milliseconds 1000
                        Write-Verbose "Bytes available: $($tcpClient.Available)"
                        If ([int64]$tcpClient.Available -gt 0) {
                            $stream = $TcpClient.GetStream()
                            $bindResponseBuffer = New-Object Byte[] -ArgumentList $tcpClient.Available
                            [Int]$response = $stream.Read($bindResponseBuffer, 0, $bindResponseBuffer.count)
                            $Null = $stringBuilder.Append(($bindResponseBuffer | ForEach {[char][int]$_}) -join '')
                        } Else {
                            Break
                        }
                    }
                    $object = [pscustomobject] @{
                        Computername = $Computer
                        Port = $_Port
                        IsOpen = $True
                        Response = $stringBuilder.Tostring()
                    }
                }
                $object.pstypenames.insert(0,'Net.TCPResponse')
                Write-Output $object
                If ($Stream) {
                    $stream.Close()
                    $stream.Dispose()
                }
                $tcpClient.Close()
                $tcpClient.Dispose()
            }
        }
    }
}

首先創(chuàng)建一個(gè)System.Net.Sockets.TCPClient對(duì)象,去連接指定的域名和端口,瞬間斷開的那是服務(wù)器沒開那個(gè)端口,直接被拒絕了,如果沒拒絕,那就等著服務(wù)器端給你響應(yīng),然后讀取字節(jié)流拼接起來進(jìn)行解析。
最后需要強(qiáng)調(diào)的是需要對(duì)打開的流和TCP連接進(jìn)行關(guān)閉,以便釋放資源
調(diào)用方法如下:

復(fù)制代碼 代碼如下:

Get-TCPResponse -Computername pop.126.com -Port 110
 

再對(duì)比一下telnet的結(jié)果

結(jié)果是一樣的,以后沒有telnet也難不住大家了,have fun!^_^

您可能感興趣的文章:
  • PowerShell小技巧之獲取Windows系統(tǒng)密碼Hash
  • PowerShell小技巧之獲取域名whois信息
  • PowerShell小技巧之實(shí)現(xiàn)文件下載(類wget)
  • PowerShell小技巧之定時(shí)抓取屏幕圖像
  • PowerShell小技巧之定時(shí)記錄操作系統(tǒng)行為
  • PowerShell小技巧之讀取Windows產(chǎn)品密鑰
  • PowerShell小技巧之發(fā)送TCP請(qǐng)求

標(biāo)簽:湛江 運(yùn)城 岳陽 濟(jì)南 德州 鶴崗 六盤水 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PowerShell小技巧之獲取TCP響應(yīng)(類Telnet)》,本文關(guān)鍵詞  PowerShell,小,技巧,之,獲取,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PowerShell小技巧之獲取TCP響應(yīng)(類Telnet)》相關(guān)的同類信息!
  • 本頁收集關(guān)于PowerShell小技巧之獲取TCP響應(yīng)(類Telnet)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章