Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Use of perl_expect in TK gui session

by kmg (Initiate)
on Sep 04, 2012 at 15:43 UTC ( #991642=perlquestion: print w/ replies, xml ) Need Help??
kmg has asked for the wisdom of the Perl Monks concerning the following question:

I am trying use the following gui program to let user to

login to a remote box dump the some process information

What I wanted is that let the perl:expect:spawn session

stdout dump on the to TK:Text window. If I use tie to display

text on the tk window, the expect:spawn is failing. If I commented

out the tie command line the code works fine, bu t out put appears

on the unix shell window and not the perl:tk:text window

How do I resolve this issue. Any help is greatly appreciated.

Thanks

Kmg

Here is the error I get when stdout is tied to tk widow using ( tie *STDOUT, 'Tk::Text', $t;)

Tk::Error: Can't locate auto/Tk/Text/CLOSE.al in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Tk/Derived.pm line 469

Carp::croak at /usr/lib/perl5/5.8.8/Carp.pm line 269

AutoLoader::AUTOLOAD at /usr/lib/perl5/5.8.8/AutoLoader.pm line 112

Tk::Derived::Delegate at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Tk/Derived.pm line 469

Tk::Widget::__ANON__ at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Tk/Widget.pm line 322

Expect::spawn at /usr/lib/perl5/vendor_perl/5.8.8/Expect.pm line 163

main::doInstallation at ./xx.pl line 61

Tk callback for .frame.button Tk::__ANON__ at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Tk.pm line 247

Tk::Button::butUp at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Tk/Button.pm line 111

<ButtonRelease-1>

(command bound to event)

X Error of failed request: BadGC (invalid GC parameter

Major opcode of failed request: 60 (X_FreeGC)

Resource id in failed request: 0x6000028

Serial number of failed request: 768

Current serial number in output stream: 567

!/usr/bin/perl -w - for linux with debugging on use Shell qw(uname pwd); use Tk; use Expect; #use strict; require Tk::Dialog; require Tk::Radiobutton; $Expect::Exp_Internal=0; $Expect::Log_Stdout=1; my $sftpusr="muser"; my $sftppswd="mpswd"; my $timeout=5; my $location=""; my $lab; my @labName = (); @labSet = ( [ "", "", "", 0], [ "m5", "192.168.200.14", "41", 1], ); ###################################################################### +# $mw = MainWindow->new(-title => "LOGVIEW"); $ft = $mw->Frame->pack(-side => 'top', -fill => 'x'); $fb = $mw->Frame->pack(-side => 'bottom', -fill => 'x'); foreach (@labSet) { push( @labName, $_->[0]); } $ft->Label(-text => "Lab Name:")->pack(-side =>'left', -anchor => 'w' +); my $opt = $ft -> Optionmenu(-options =>\@labName, -command => \&displayLoads, -variable => \$location, -relief +=> 'sunken')->pack(-side =>'left', -anchor => 'w'); $fb->Button(-text => "Exit", -command => sub{exit;})->pack( -side => ' +right'); $ft->Button(-text => "Display", -command => \&displayInfo )->pack( -si +de => 'right'); $t = $mw->Scrolled("Text")->pack(-side => 'left', -fill => 'x', -expan +d => 1); $ssh = new Expect; # forever loop MainLoop; ###################################################################### +############################ sub displayLoads { foreach( @labSet) { if ( $_->[0] eq $location ) { $lab = $_; last; } } }sub displayInfo { tie *STDOUT, 'Tk::Text', $t; my $parms=sprintf("%s@%s", ${sftpusr}, $lab->[1]); print "**** pram $parms\n"; $ssh->spawn("ssh", $parms) or die "Cannot ssh to $lab->[1]\n"; $ssh->expect( $timeout, # ["Password Authentication"], ["Are you sure you want to continue connecting (yes/no +)?", sub { my $self = shift; $self->send("yes\n"); exp_continue;} ], [".*password: ", sub { my $self = shift; $self->send(" +$sftppswd\n"); exp_continue; } ], [ 'timeout', sub { print "Error ssh login timeout\n"; +exit} ], [".*-root>"], ); # check board type $ssh->send( "cat \/proc\/cpuinfo | grep platform | awk \'{prin +t \$3}\'\n"); $ssh->expect( $timeout, ["XXXX", sub { my $self = shift; print "\n*** FAILED: +Target board is not p$lab->[2] cpu **\n"; $self->soft_close(); exit( +); } ], # [".*-root>"] , [ 'timeout', sub { print "Error ssh cmd cpuinfo timeou +t\n"; exit(); } ] ); }

Comment on Use of perl_expect in TK gui session
Download Code
Replies are listed 'Best First'.
Re: Use of perl_expect in TK gui session
by eskiphill (Initiate) on Sep 04, 2012 at 23:32 UTC
    $t is a reference to an element on the window. You would use it if you wanted to delete the scroll box from the window. It is not a reference to the contents of the element. You need to add -textvariable -> $windowcontents to the definition statement for the scrolled window so you have access to the contents of that window. (maybe -variable.. ) Then you can stuff the results of the expect process into that variable so it can be seen on the window.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991642]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-30 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls