powershell を使用して telnet サーバーに接続する方法を見つけました。このスクリプトにより、tcp ソケット タイプの接続を作成し、コマンドを渡すことができます。
基本的に、System.Net.Sockets.TcpClient System.IO.StreamWriter を使用してから、一連の WriteLine メソッドと Flush メソッドを使用します。
WriteLine を使用して通常の ascii タイプの行を渡すことに問題はありません。しかし、どうすれば F3 キーを渡すことができますか。
F3 には ASCII コードがなく、F3 を押したときにどのバイト シーケンスが telnet に送信されるかわかりません。telnet セッションで特定のタスクを実行するには、F3 キーが必要です。
どんな援助でも大歓迎です。これが私のコードの例です:
$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