2

我想出了一种使用 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
4

2 回答 2

0

与控制键组合(ctrl-v、ctrl-c 等)一样,可以发送特殊序列来完成任务。

可以在这里找到一个很好的总结,其他网站显示相同的结果。

但是,我不确定这是否依赖于互联网标准或实现/shell,因为我在 RFC 中没有发现任何关于此的内容。

请注意,windows cmd(和powershell)使用F3来粘贴最后执行的命令,因此您必须调用F3的传输否则。

于 2013-09-13T19:32:24.870 回答
0

这修复了它。我可以通过这个脚本将 F3 发送到远程登录会话。注意 $z 的字节,我关闭了底部的套接字。

我不得不使用wireshark来找出他的字节。

[Byte[]] $z = 0x1b, 0x4f, 0x52


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)
    $BinWriter.Flush()
    Start-Sleep -Milliseconds $WaitTime     
    $BinWriter.Write($z)
    $BinWriter.Flush()
    Start-Sleep -Milliseconds $WaitTime 

    #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))
    }
    $Socket.Close()
}
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
于 2013-09-20T17:09:18.340 回答