我试图让Telnet连接到Windows Server 2012 R2,Ubuntu 16.04和Windows XP SP3的PowerShell自动化,并且所有服务器都将垃圾字符作为输出。
我正在经历rfc854,但我无法完成这项工作。
您能否帮我解决一下如何解决这个问题。
下面的代码会尝试使用凭据和输出目录列表连接Telnet服务器。
代码
param (
[string] $terminalServer = "192.168.19.131",
[int] $port = 23,
[string] $username = "username",
[string] $password = "password",
[int] $commandDelay = 1000,
[string] $output = ""
)
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 = 2000
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
}
$output = ""
write-host "Connecting to $terminalServer on port $port..."
trap { Write-Error "Could not connect to the server: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($terminalServer, $port)
write-host "Connected. `n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$SCRIPT:output += GetOutput
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
$writer.WriteLine("dir")
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
$writer.WriteLine("exit")
$writer.Flush()
Start-Sleep -m $commandDelay
## Close the streams
$writer.Close()
$stream.Close()
write-host "All streams are closed. The final output is"
$output
输出
D:\Tools\T24\PowerShell>powershell .\telnet_test.ps1
Connecting to 192.168.19.131 on port 23...
Connected.
All streams are closed. The final output is
??%??????'???? ??
答案 0 :(得分:0)
我已尝试在Windows Server 2012 R2上使用jBASE Telnetd Server V4.1.1并完美列出目录。
因此,根据rfc854实现,Windows版本的Telnet需要更多操作。