简体   繁体   中英

Perl Issue with linux @ Net::Telnet and vt-100 terminal

I need help with Net:Telnet. Trying to connect to VT-100 terminal. When using normal telnet from console it works fine. I can connect and issue commands, but when i try the same from perl, it connects, but then nothing happens - i have no output or waitfor just timeouts or displays that prompt is not matched (but it its, beause its always three letters and > sign). I'v tried all possible ways -> using methods like print, cmd, put, but without any success and stuck for 2 days on that thing.

Here is my sample code:

use Net::Telnet ();
my $conn = new Net::Telnet (TelnetMode => 0);

unless ($conn->open(Host => $host, Port => 23))
{
    die "Error opening socket:: ".$conn->errmsg();
}

print "Connected to ".$conn->host().", port ".$conn->port()."\n";

$conn->prompt('/...>/'); # prompt is 014>

my $lines = $conn->put("15\r"); # command 15 to give me the site name
my ($a,$b);
($a,$b) = $conn->waitfor(Match=>'/...>/',Timeout=>100);

$conn->close;

When wrong command is entered, normally telnet displays error message and prompt, but from Perl it does nothing. Input log is empty, output log is empty and dump log contains only following:

0x00000: 31 35 0d 0a 15..

I dont know what else to try.

Here is how looks normal telnet session output:

Trying xxx.xxx.xxx.xxx...
Connected to xxx.xxx.xxx.xxx.
Escape character is '^]'.

014>15
15
014 TMR Name

014>^]
telnet> quit
Connection closed.

Any help is appreciated

I only have experience using Net::Telnet for connecting to routers and switches and have never had issues.

While I have no experience with a terminal of this type, a few things stand out. For example TelnetMode => 0 disables telnet control sequences from being interpreted. This should only be done if you're using Net::Telnet to connect to a non-telnet service.

Other stuff that looks off to me:

  • The default Port is 23 and the default Errmode is 'die' so those are kind of redundant.
  • The put function doesn't return output, you need to use cmd
  • Don't ever use $a and $b as they are special variables used by perl for sorting.

This may not work, but if I were re-writing your script using the idioms that have worked fine for me, I would write it like this...

use strict;
use warnings;
use Net::Telnet;

my $prompt = '/.+[:#>] *$/';
my $conn = new Net::Telnet (Prompt => $prompt);

$conn->open($host); # WILL DIE IF CONNECTION FAILS

# AUTHENTICATION GOES HERE IF NEEDED
# $conn->login($username, $password);

print "Connected to ".$conn->host().":".$conn->port()."\n";

my @lines = $conn->cmd("14");
print for @lines;

$conn->close;

I'm usually parsing @lines ... If you just want to print the output, then you don't need an interim variable and can just print for $conn->cmd("14");

If the system is particularly slow, initialise the connection with a longer timeout, ie my $conn = new Net::Telnet (Prompt => $prompt, Timeout => 20);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM