Passing F keys to Emulated Telnet session in Powershell (TCP)
I have figured out a way to connect to a telnet server using powershell.
The script allows to create a tcp socket type connections and pass
commands.
Basically, I use System.Net.Sockets.TcpClient System.IO.StreamWriter and
then a sequence of WriteLine and Flush methods.
I have no problem passing regular ascii type lines using WriteLine. But
how do I pass on the F3 key.
F3 has no ASCII code, and I don't know what byte sequence is sent to
telnet when I hit F3. I need F3 to do certain tasks in the telnet session.
Any assistance is highly appreciated. Here is an example of my code:
$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
No comments:
Post a Comment