M
Mike
Hi, I have been trying to create a Tk::Text that reads and writes to a
Net::Telnet object. It works fairly well, but doesnt handle terminal
types of programs like vi.
Just looking for any ideas from the experts. I dont know to much about
termianl emulations, etc.
Also, i get two prompts. Anyone know why i might be getting that from
Net::Telnet; I have prompt set to anything and timeout at 0, so i dont
match on prompt or block. I just poll every 1/4 sec. But what i read
with get, comes with two prompts. Any ideas on a better method let me
know. I was trying to keep from using prompt, allowing it to work no
matter what you logged into.
This script is an example of its use followed by the module itself
Note the ip is hardcoded so change to fit your system.
Thanks
Mike
############################################################
use strict;
use Tk;
use lib 'lib';
use Tk::ECC::Telnet;
my $mw = new MainWindow();
my $tn = $mw->Telnet->pack(-side => 'top', -expand => '1', -fill =>
'both');
my $bt = $mw->Button(-text => 'Connect', -command => [\&connect,
$tn])->pack(-expand => '1', -fill => 'x');
MainLoop;
sub connect {
my $tn = shift;
print "Connecting to $tn\n";
$tn->conn('10.1.24.226');
}
##############################################################
Here is the Module Tk::ECC::Telnet
# -------------------------------------------------------------------
# Tk Widget based on Standard Tk::Text and Net::Telnet
# -------------------------------------------------------------------
package Tk::ECC::Telnet;
use Net::Telnet;
use base qw(Tk:
erived Tk::Text);
Construct Tk::Widget 'Telnet';
sub ClassInit {
my ($class, $mw) = @_;
$class->SUPER::ClassInit($mw);
$mw->bind($class,'<Return>', \&Return)
#$mw->bind($class, '<KeyPress>', \&keypress);
}
sub Populate {
my ($self, $args) = @_;
my $ip = '192.168.2.10';
$self->SUPER:
opulate($args);
$self->{END} = 0.0;
$self->{KEY} = 0;
$self->{TELNET} = new Net::Telnet( Timeout => 0, Prompt => '//',);
$self->{TELNET}->errmode("return");
}
sub conn {
my ($self, $ip) = @_;
$self->{TELNET}->open($ip);
$self->repeat('250', [\&read_telnet, $self])
}
sub disconn {
print "Disccc\n";
my ($self,) = shift;
$self->{TELNET}->print("exit");
#$self->cancel('250', [\&read_telnet, $self])
}
sub insert {
#print "insert [@_]\n";
my ($self) = shift;
$self->SUPER::insert(@_);
return if ($self->{KEY});
print "Setting End\n";
$self->{END} = $self->index('insert');
}
sub Return {
#print "Return [@_]\n";
my $self = shift;
return unless ($self->compare($self->index('insert'), '>=',
$self->{END}));
my $cmd = $self->get($self->{END}, 'end');
$self->SUPER::Insert("\n");
$self->{TELNET}->print($cmd);
$self->{TELNET}->timeout(1);
$self->{TELNET}->getline();
$self->{TELNET}->getline();
$self->{TELNET}->timeout(0);
}
sub Backspace {
#print "BackSpace [@_]\n";
my $self = shift;
print $self->index('insert') . ">" . $self->{END} . "\n";
return unless ($self->compare($self->index('insert'), '>',
$self->{END}));
$self->SUPER::Backspace;
}
sub InsertKeypress {
#print "InsertKeypress [@_]\n";
my ($self, $key) = @_;
return unless ($self->compare($self->index('insert'), '>=',
$self->{END}));
$self->{KEY} = 1;
$self->SUPER::InsertKeypress($key);
$self->{KEY} = 0;
#print "END is currently " . $self->{END} . "\n";
}
sub read_telnet {
my ($self) = shift;
while (my $data = $self->{TELNET}->get()) {
print "Data [$data]\n";
$self->insert('end', $data);
$self->see('end');
$self->markSet('insert', 'end');
}
}
1;
Net::Telnet object. It works fairly well, but doesnt handle terminal
types of programs like vi.
Just looking for any ideas from the experts. I dont know to much about
termianl emulations, etc.
Also, i get two prompts. Anyone know why i might be getting that from
Net::Telnet; I have prompt set to anything and timeout at 0, so i dont
match on prompt or block. I just poll every 1/4 sec. But what i read
with get, comes with two prompts. Any ideas on a better method let me
know. I was trying to keep from using prompt, allowing it to work no
matter what you logged into.
This script is an example of its use followed by the module itself
Note the ip is hardcoded so change to fit your system.
Thanks
Mike
############################################################
use strict;
use Tk;
use lib 'lib';
use Tk::ECC::Telnet;
my $mw = new MainWindow();
my $tn = $mw->Telnet->pack(-side => 'top', -expand => '1', -fill =>
'both');
my $bt = $mw->Button(-text => 'Connect', -command => [\&connect,
$tn])->pack(-expand => '1', -fill => 'x');
MainLoop;
sub connect {
my $tn = shift;
print "Connecting to $tn\n";
$tn->conn('10.1.24.226');
}
##############################################################
Here is the Module Tk::ECC::Telnet
# -------------------------------------------------------------------
# Tk Widget based on Standard Tk::Text and Net::Telnet
# -------------------------------------------------------------------
package Tk::ECC::Telnet;
use Net::Telnet;
use base qw(Tk:
Construct Tk::Widget 'Telnet';
sub ClassInit {
my ($class, $mw) = @_;
$class->SUPER::ClassInit($mw);
$mw->bind($class,'<Return>', \&Return)
#$mw->bind($class, '<KeyPress>', \&keypress);
}
sub Populate {
my ($self, $args) = @_;
my $ip = '192.168.2.10';
$self->SUPER:
$self->{END} = 0.0;
$self->{KEY} = 0;
$self->{TELNET} = new Net::Telnet( Timeout => 0, Prompt => '//',);
$self->{TELNET}->errmode("return");
}
sub conn {
my ($self, $ip) = @_;
$self->{TELNET}->open($ip);
$self->repeat('250', [\&read_telnet, $self])
}
sub disconn {
print "Disccc\n";
my ($self,) = shift;
$self->{TELNET}->print("exit");
#$self->cancel('250', [\&read_telnet, $self])
}
sub insert {
#print "insert [@_]\n";
my ($self) = shift;
$self->SUPER::insert(@_);
return if ($self->{KEY});
print "Setting End\n";
$self->{END} = $self->index('insert');
}
sub Return {
#print "Return [@_]\n";
my $self = shift;
return unless ($self->compare($self->index('insert'), '>=',
$self->{END}));
my $cmd = $self->get($self->{END}, 'end');
$self->SUPER::Insert("\n");
$self->{TELNET}->print($cmd);
$self->{TELNET}->timeout(1);
$self->{TELNET}->getline();
$self->{TELNET}->getline();
$self->{TELNET}->timeout(0);
}
sub Backspace {
#print "BackSpace [@_]\n";
my $self = shift;
print $self->index('insert') . ">" . $self->{END} . "\n";
return unless ($self->compare($self->index('insert'), '>',
$self->{END}));
$self->SUPER::Backspace;
}
sub InsertKeypress {
#print "InsertKeypress [@_]\n";
my ($self, $key) = @_;
return unless ($self->compare($self->index('insert'), '>=',
$self->{END}));
$self->{KEY} = 1;
$self->SUPER::InsertKeypress($key);
$self->{KEY} = 0;
#print "END is currently " . $self->{END} . "\n";
}
sub read_telnet {
my ($self) = shift;
while (my $data = $self->{TELNET}->get()) {
print "Data [$data]\n";
$self->insert('end', $data);
$self->see('end');
$self->markSet('insert', 'end');
}
}
1;