Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Telnet I/O with Perl/Tk

138 views
Skip to first unread message

Matt

unread,
Mar 1, 2011, 2:04:44 AM3/1/11
to
Hi guys,

Wondering if someone might be able to give me some help, I have a
simple Perl program to control a telnet session, using Net::Telnet I
would like to have I/O redirected to a Tk GUI, but am a little
confused about how to this. I have the Tk GUI written, just confused
about what type of widget I should use in the GUI and how to get the
Input and Output from my terminal to the gui.

Thanks.

$Bill

unread,
Mar 1, 2011, 7:11:49 PM3/1/11
to

I'm just guessing here, but I think I'd just use a scrolled list box
( $listBox = ??->Scrolled('Listbox', ... ) and feed the output of you
$telnet->cmd call returns into the box (possibly interspersed with the
commands you sent (possibly with different markings or colors to denote
cmds from replies) using a $ListBox->insert('end', ... ). Hope that
makes sense.

Jack

unread,
Mar 1, 2011, 11:34:24 PM3/1/11
to

Another option is to dump the output to a Text widget.

Opt 1) I have never used Net::Telnet before - but it seems you should be
able to capture the output and "manually" write it to the text widget.
The drawback is that this would one way - writing.

Opt 2) You didn't mention your platform - but if it is NOT windows AND
you are willing to forego Net::Telnet, then you can also try launching a
telnet session using IPC::Open2 and use fileevent to trigger the writing
to the Text widget.This would also allow input to be read if you trigger
reading from the text widget (trigger on hitting the Enter key). This
should offer you two way input/output.

http://perlmonks.org/?node_id=452894

Opt 3) Embed an actual xterm into a Tk window (Frame) using the
-container option and the xterm -into option. The last time I tried to
do this was a number of years ago and I didn't like how it remained a
fixed size. This way you can type your commands in to an actual terminal
window.

Jack

Matt

unread,
Mar 3, 2011, 11:44:53 AM3/3/11
to

Hi,

Thanks very much for the help-I decided to experiment with embedding
the xterm in the Tk window. This worked, but I am using Tk::NoteBook
and it seems that the SAME xterm process gets embedded in each tab of
the NoteBook. Is there a way to insert a different xterm process into
each page of the notebook?

Thanks

Steve C

unread,
Mar 3, 2011, 3:23:58 PM3/3/11
to

Barring a cosmic shift in reality, Tk::Notebook is doing what your
program tells it to. Post some code and let's see.


Matt

unread,
Mar 3, 2011, 4:27:41 PM3/3/11
to

Ah yes, sorry.

#$mw is the main window variable
#@hosts is the array contain each host to telnet to.

my $canv = $mw->Canvas(-bg => 'lightsteelblue',
-relief => 'sunken',
-width => 800,
-height => 600)->pack(-expand => 1, -fill => 'both');

my $xtermWidth = 400;
my $xtermHeight = 300;

my $book = $canv->NoteBook()->pack(-fill=>'both', -expand=>1);

for my $host (@hosts) {
my $tab = $book->add ("$host", -label => $host, -createcmd=>sub
{ create($host) });
}

MainLoop;

sub create {
my $host = shift;

#This code from perlmonks on embedding xterm windows:

## this Frame is needed for including the xterm in Tk::Canvas
my $xtermContainer = $canv->Frame(-container => 1);
my $xtid = $xtermContainer->id();
# converting the id from HEX to decimal as xterm requires a
decimal Id
+
my ($xtId) = sprintf hex $xtid;

my $dcontitem = $canv->createWindow(275,175,
-window => $xtermContainer,
-width => $xtermWidth+100,
-height => $xtermHeight,
-state => 'normal');
system("xterm -into $xtId -hold -e \'telnet $host; bash\' &");
}


I have tried changing the value of $xtId to try and force it to
recognize multiple sessions, but that prevents the terminal window
from being created at all. Am I perhaps missing something in the
NoteBook tab generation to recognize multiple xterms?

Thanks.

Jack

unread,
Mar 3, 2011, 8:51:51 PM3/3/11
to

I see the problem right away. You are packing a Frame into a "window"
into a canvas. And you are doing the same thing in the same place for
each of your hosts.

I think you really want to pack the xterm into each frame of the
Notebook right? Remember that when you "add" a page in a Notebook it
returns a Tk::Frame. I believe though that the -container option *must*
be specified at creation. (i.e. You cannot configure it afterwards
according to the documentation for Tk::Frame). I am not sure if the
"add" method will support this. Typically any extra options will be
passed to the base widget. If not - then you can just as easily pack
another frame into each page of the notebook using the -container option
- then use *that* winID in your xterm command.

I'm on windows right now - but I'll log into work to see if I can come
up with some working code using a linux machine.

Stay tuned - unless someone (even yourself) beats me to it ;)

Jack

Jack

unread,
Mar 3, 2011, 10:19:24 PM3/3/11
to

Okay here is a working example:

#!/usr/bin/perl -w

use strict;
use Tk;
use Tk::NoteBook;
my %page;

my $mw = tkinit;
my $nb = $mw->NoteBook()->pack(-expand=>1, -fill=>'both');

foreach my $page (1..4) {
$page{$page} = $nb->add("page$page", -label=>"Session $page");
doXterm($page{$page});
}

MainLoop;

sub doXterm {
my ($pageframe) = @_;

my $intframe = $pageframe->Frame(
-container=>1,
-width=>600,
-height=>400)->pack(-expand=>0, -fill=>'both');
my $ID = $intframe->id();
my ($hexID) = sprintf hex $ID;

system("xterm -into $hexID -geometry 100x40 -hold &"); #geom colsxrows
}
__END__

I have left the "telnet" stuff up to you.

Jack

0 new messages