Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

telnet

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?

like image 924
root1982 Avatar asked Feb 25 '10 16:02

root1982


2 Answers

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

like image 89
root1982 Avatar answered Sep 19 '22 14:09

root1982


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

like image 22
Leon Timmermans Avatar answered Sep 20 '22 14:09

Leon Timmermans