読者です 読者をやめる 読者になる 読者になる

qshinoの日記

Powershell関係と徒然なこと

TCPconnection by Powershell

TCP by Powershell

手順

  1. new tcpclient
  2. stream to reader
  3. check .Connected
  4. read(buffer,0,size)
  5. Write
$tcpConnection = New-Object System.Net.Sockets.TcpClient($FTPServer, $FTPPort)
$tcpStream = $tcpConnection.GetStream()
$reader = New-Object System.IO.StreamReader($tcpStream)
$writer = New-Object System.IO.StreamWriter($tcpStream)
$writer.AutoFlush = $true

$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding 

while ($tcpConnection.Connected)
{
    while ($tcpStream.DataAvailable)
    {

        $rawresponse = $reader.Read($buffer, 0, 1024)
        $response = $encoding.GetString($buffer, 0, $rawresponse)   
    }

    if ($tcpConnection.Connected){
        Write-Host -NoNewline "prompt> "
        $command = Read-Host

        if ($command -eq "escape"){
            break
        }

        $writer.WriteLine($command) > $null
    }
    start-sleep -Milliseconds 500
}

$reader.Close()
$writer.Close()
$tcpConnection.Close()