How can I fix "unknown terminal type" when connecting with Perl's Net::Telnet?

10,976

Solution 1

If you use the option_log function to log the telnet options received and returned. You will see that Perl does not send the terminal type to the server by default. Server will default the terminal type to "network" for some reasons.

The right way to do this is to set the terminal type on the perl side.

my $termtype = 'vt100'; my $telopt_ttype_ok = '';
my $t = new Net::Telnet (Timeout => 5);

$t->option_callback(\&opt_callback);
$t->option_accept(Do=>Net::Telnet->TELOPT_TTYPE);
$t->suboption_callback(\&subopt_callback);

$t->open($server);
$t->input_log("runRemoteCommand_telnet.log");
$t->login($user, $pass);
$t->cmd($command);

exit 0;

sub opt_callback { 
  my ($obj, $option, $is_remote, $is_enabled, $was_enabled, $buf_position) = @_;
  if ($option == Net::Telnet->TELOPT_TTYPE and $is_enabled and !$is_remote) {
    $telopt_ttype_ok = 1; 
   }
   1;
}

sub subopt_callback {
 my ($obj, $option, $parameters) = @_; 
 my ($ors_old, $otm_old);
 if ($option == Net::Telnet->TELOPT_TTYPE) 
 {
  $ors_old = $obj->output_record_separator('');
  $otm_old = $obj->telnetmode (0);
  $obj->print("\xff\xfa", pack("CC", $option, 0), $termtype, "\xff\xf0");
  $obj->output_record_separator($ors_old);
  $obj->telnetmode ($otm_old);
 }
 1;
}

Refer to this

Solution 2

See this discussion for the same problem. The solution (untested) that they propose is to set the TERM enviromental variable to a known value, such as TERM=vt100

Solution 3

What works too is to add the question mark to your prompt value and then send as the first command the echo $TERM value. So if your command prompt is '>' and echo $TERM gives 'xterm':

my $t = new Net::Telnet (   Timeout => 3,
                            Prompt => '/[\?>]/',
                            Dump_log => $dumplog,
                            Host => $host,
                            Input_log => $inputlog,
                            Output_log => $outputlog,
                    );

$t->login($username, $password);
my @lines = $t->cmd("xterm");
@lines = $t->cmd("who");
print @lines, "\n";
@lines = $t->cmd("uptime");
print @lines, "\n";
Share:
10,976
root1982
Author by

root1982

Updated on June 12, 2022

Comments

  • root1982
    root1982 almost 2 years

    I got a problem connecting to a SUSE linux machine by Perl's Net::Telnet module. The code looks like below:

    my $t = new Net::Telnet (Timeout => 20);
    $t->open($server);
    $t->input_log("telnet.log");
    $t->login($user, $pass);
    my @lines=$t->cmd($command);
    print @lines;
    

    The log file looks like below: Welcome to SUSE Linux Enterprise Server 10 SP1 (x86_64) - Kernel 2.6.16.46-0.12-default (5).

    vm-sles10u5 login: <myuser>
    Password: 
    Last login: Thu Feb 25 10:41:07 EST 2010 from <mymachine> on pts/5
    tset: unknown terminal type network
    Terminal type? 
    

    Any suggestions?