我如何在 Net::Telnet 的 Perl 中只发送一个 CR,而不是一个 CR NUL?

How do i send just a CR, not a CR NUL in Perl with Net::Telnet?

我需要通过 telnet 协议与设备通信。事实证明它不能处理 CR-NUL 或 CR-LF 序列作为输出记录分隔符,只能直接 CR。否则它会将 CR 后面的字符解释为属于下一个命令,从而导致错误。所以我将 Output_record_separator 设置为 CR,但随后在通信转储中看到(在 Dump_Log 中),不仅发送了 CR (0x0d),还发送了 CR NUL (0x0d 0x00)。不过,我可以发送单个字符:如果我只发送一个 LF,则只发送一个 LF (ox0a)。

这段代码显示了问题:

#!/usr/bin/env perl
use strict;
use warnings;

use Net::Telnet;

my $host = "192.168.2.223";
my $port = 5000;


my $telnet = new Net::Telnet ( 
    Timeout                 => 20, 
    Errmode                 => 'die',
    Port                    => $port,
    Dump_Log                => '/tmp/debug',
    Output_record_separator => "5",
    Input_record_separator  => "\n\r",
    Prompt                  => '/1_.*0/',
    Binmode                 => 1,
);

$telnet->open($host);
$telnet->put("5");
$telnet->put("2");

结果是:

tail: /tmp/debug: file truncated
> 0x00000: 0d 00                                               ..

> 0x00000: 0a                                                  .

如何让 Net::Telnet 只发送 CR,而不添加更多内容?

Telnetmode 设置为 0。如果 Telnetmode 开启,则 CR 转换为 CR + 0x00。

来自 Net::Telnet 来源:

## Convert all CR (not followed by LF) to CR NULL.
while (($pos = index($$string, "5", $pos)) > -1) {
$nextchar = substr $$string, $pos + 1, 1;

substr($$string, $pos, 1) = "5[=10=]0"
    unless $nextchar eq "2";

您可以使用 $telnet->telnetmode(...):

打开和关闭该模式

telnetmode - turn off/on telnet command interpretation
$mode = $obj->telnetmode;
$prev = $obj->telnetmode($mode);

This method controls whether or not TELNET commands in the data stream are recognized and handled. The TELNET protocol uses certain character sequences sent in the data stream to control the session. If the port you're connecting to isn't using the TELNET protocol, then you should turn this mode off. The default is on.

If no argument is given, the current mode is returned.

If $mode is 0 then telnet mode is off. If $mode is 1 then telnet mode is on.