我想出了一种使用 powershell 连接到 telnet 服务器的方法。该脚本允许创建 tcp 套接字类型的连接并传递命令。
基本上,我使用 System.Net.Sockets.TcpClient System.IO.StreamWriter,然后使用一系列 WriteLine 和 Flush 方法。
使用 WriteLine 传递常规 ascii 类型行没有问题。但是我如何传递 F3 键。
F3没有ASCII码,不知道按F3时发送到telnet的字节序列是什么。我需要 F3 在 telnet 会话中执行某些任务。
非常感谢任何帮助。这是我的代码示例:
$x = [char]114 + [char]189 <-- F3 key ???????????????????
Function Get-Telnet
{ Param (
[Parameter(ValueFromPipeline=$true)],
[string]$RemoteHost = "HostnameOrIPAddress",
[string]$Port = "23",
[int]$WaitTime = 6000,
[string]$OutputPath = ""
)
#Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
If ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
#Now start issuing the commands
$Writer.WriteLine([char]08)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$Writer.WriteLine("TA")
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$Writer.WriteLine("12345")
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$Writer.Write($x)
$Writer.Flush()
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
#Save all the results
While($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
}
Else
{ $Result = "Unable to connect to host: $($RemoteHost):$Port"
}
#Done, now save the results to a file
$Result | Out-File $OutputPath
}
Get-Telnet -RemoteHost "localhost" -OutputPath c:\windows\temp\telnetlog.imp
更新:
更新代码以输入 F3,见下文。telnet 会话仍然没有收到 F3。其他所有键都有效。F3 基本上会注销此特定 telnet 应用程序的 telnet 会话。
$y = "\033[13~"
$z = [System.Text.Encoding]::ASCII.GetBytes($y)
$z
Function Get-Telnet
{ Param (
[Parameter(ValueFromPipeline=$true)]
[String[]]$Commands = @([char]08,"TA","12345","N","N","N"),
[string]$RemoteHost = "HostnameOrIPAddress",
[string]$Port = "23",
[int]$WaitTime = 6000,
[string]$OutputPath = "\\server\share\switchbackup.txt"
)
#Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
If ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$BinWriter = New-Object System.IO.BinaryWriter($stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
#Now start issuing the commands
$Writer.WriteLine([char]08)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$Writer.WriteLine("TA")
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$Writer.Write("12345")
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
$BinWriter.Write($z)
# $Writer.Write($z)
$BinWriter.Flush()
Start-Sleep -Milliseconds 500
$BinWriter.Write($z)
$BinWriter.Flush()
Start-Sleep -Milliseconds 500
#All commands issued, but since the last command is usually going to be
#the longest let's wait a little longer for it to finish
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
#Save all the results
While($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
}
Else
{ $Result = "Unable to connect to host: $($RemoteHost):$Port"
}
#Done, now save the results to a file
$Result | Out-File $OutputPath
}
#Get-Telnet -RemoteHost localhost -Commands "TA" -OutputPath c:\windows \temp\telnetlog.imp
Get-Telnet -RemoteHost "localhost" -OutputPath c:\windows\temp\telnetlog.imp