?
Solved

Cannot get telnet session output using TK

Posted on 2006-06-08
3
Medium Priority
?
281 Views
Last Modified: 2012-05-05
PC platform Windows XP
Perl 5.8.7 ActiveState

I am trying to get my TK GUI to print output from a device via telnet.  However, I get the following output on the application...

Entered loginG6, IP address is 10.2.161.209.
Net::Telnet=GLOB(0x1eacbdc)
Net::Telnet=GLOB(0x1eacbdc)
blah is 1
Entered command show ver all
Net::Telnet=GLOB(0x1eacbdc)

Entire Code....

#!c:\perl\bin\perl.exe
use Tk;

my $mw = new MainWindow(-title=>"G6 Telnet");; # Main Window
my $frm_name = $mw -> Frame();
my $lab = $frm_name -> Label(-text=>"SCM IP Address:");
my $ent = $frm_name -> Entry();

my $frm_name2 = $mw -> Frame();
my $lab2 = $frm_name2 -> Label(-text=>"SCM Command:");
my $ent2 = $frm_name2 -> Entry();

my $but = $mw -> Button(-text=>"Push Me", -command =>\&loginG6);

my $textarea = $mw -> Frame(); #Creating Another Frame
my $txt = $textarea -> Text(-width=>40, -height=>10);
my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $txt]);
my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $txt]);
$txt -> configure(-yscrollcommand=>['set', $srl_y],
            -xscrollcommand=>['set',$srl_x]);

$lab -> grid(-row=>1,-column=>1);
$ent -> grid(-row=>1,-column=>2);
$frm_name -> grid(-row=>1,-column=>1,-columnspan=>2);
$lab2 -> grid(-row=>2,-column=>1);
$ent2 -> grid(-row=>2,-column=>2);
$frm_name2 -> grid(-row=>2,-column=>1,-columnspan=>2);

$but -> grid(-row=>4,-column=>1,-columnspan=>2);

$txt -> grid(-row=>1,-column=>1);
$srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns");
$srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew");
$textarea -> grid(-row=>5,-column=>1,-columnspan=>2);

MainLoop;


  #-------------------------------------------------------------------
  # Subroutine: loginG6
  # Version Added: 1.0
  # Input: n/a
  # Output:
  # Description:
  #-------------------------------------------------------------------

sub loginG6 {
             use Net::Telnet;
             use Socket;
             $tn = new Net::Telnet ( Timeout=>10,
                                           Errmode=> sub {&noconn;});
           my $scmip = $ent -> get();
           my $cmd = $ent2 -> get();

           $txt -> insert('end',"Entered loginG6, IP address is $scmip.\n"); # this prints correctly "Entered loginG6, IP address is 10.2.161.209."
             $tn->open($scmip);
             $tn->waitfor('/Login[: ]$/i');
             $txt -> insert('end',"$tn\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"
             $tn->print("****");
             $tn->waitfor('/Password[: ]*$/i');
             $txt -> insert('end',"$tn\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"
             $tn->print("****");
             $blah=$tn->waitfor('/.+/i');
             $txt -> insert('end',"blah is $blah\n");  # this prints incorrectly "blah is 1"
             autoAnswer("set cli more off");
#             $tn->waitfor('/CLI/i');
#             $txt -> insert('end',"$tn");
             autoAnswer("set ev di all on");
#             $tn->waitfor('/CLI/i');
#             $txt -> insert('end',"$tn");
             autoAnswer("set al di on");
#             $tn->waitfor('/CLI/i');
#             $txt -> insert('end',"$tn");
             $tn->print($cmd);
             $tn->waitfor('/.+/i');
            $txt -> insert('end',"Entered command $cmd\n"); # this prints correctly "Entered command show ver all"
             $txt -> insert('end',"$tn\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"
            
}

  #-------------------------------------------------------------------
  # Subroutine: noconn
  # Version Added: 1.0
  # Input: n/a
  # Output:
  # Description: Reports to user that no connection was made to device
  #-------------------------------------------------------------------

sub noconn {
      print "\n";
      print "##################################################################\n";
      print "ERROR\n";
      print "Cannot connect to devise at $scmip.  What is up with that?.\n";
      print "##################################################################\n";
      exit;
}

  #-------------------------------------------------------------------
  # Subroutine: autoAnswer
  # Version Added: 1.0
  # Input: n/a
  # Output:
  # Description:
  #-------------------------------------------------------------------

    sub autoAnswer  {
#    print "hitting autoanswer with, $cliCmd<br>";
      my ($cliCmd) = @_;
#      print "$cliCmd<br>";
      $tn->print($cliCmd);
      $line = " ";
      while ($line !~ /Command Completed/) {
         if ($line =~ /you sure/) {
           $tn->print ("y");
         }
         $line = $tn->getline();
      }
    }
0
Comment
Question by:lormand
3 Comments
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16866846
where you say prints incrrectly:

$txt -> insert('end',"$tn\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"
$txt -> insert('end',"$tn\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"

what do you think or expect should be printed there? $tn is a reference to the Net::Telnet object you created, thats all you are printing, the value of the reference. Maybe you should be assigning the return value to a scalar, for example:

$open = $tn->open($scmip);

and then:

$txt -> insert('end',"$open\n");  # this prints incorrectly "Net::Telnet=GLOB(0x1ead070)"

or whatever is appropriate for what you want to print at that point in the script.
0
 

Author Comment

by:lormand
ID: 16867086
In my script, I assign a scalar to the telnet object as you suggest.

Example below from above script...

$blah=$tn->waitfor('/.+/i');
$txt -> insert('end',"blah is $blah\n");

In this case, I get "blah is 1".

I attempted to print "$tn" in the TK object, because when I use it in an ordinary command line perl script, it prints every character returned up to the regex.  

For example,

print  $tn->waitfor('/CLI/i');

when using in CGI or CLI will print to console all characters until reaching "CLI".  But assigning it to a scalar and printing to TK GUI shows "1".
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1000 total points
ID: 16867273
Pretty sure the methods return a list:

  @blah = $tn->waitfor('/.+/i');
  $txt -> insert('end',"blah is ".join("",@blah)."\n");

or

  $txt -> insert('end', "blah is ".join("", $tn->waitfor('.+/i'))."\n");

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question