Solved

Sending F3 to ksh using Net::Telnet

Posted on 2006-11-26
23
619 Views
Last Modified: 2012-06-21
I've written a script to telnet to a box and run a grep command and save it into a list, but our server is a unix box and requires you to press F3 before it gives you the shell prompt. I can't figure out how to give it an F3 from perl. Any help would be appreciated. Here's the code.

#!perl


use strict;
use warnings;
use diagnostics;

use lib '/server/share/';
use Net::Telnet ();

my $login = "name";
my $pass = "pass";
my $ttyType = "vt100";
my $profs_id = "username";

#Begin Telnet

print "Wrong number of arguments" and die "$!\n" unless @ARGV > 0;

my $input = shift;
my $isp = "server.s0".$input;

my $t = new Net::Telnet (Timeout => 50,
                 Prompt => '/\$ $/');
                                     
                                    
$t->open($server);

$t->waitfor('/login: $/i') or die $t->lastline;
$t->print($login);
print $t->lastline;

$t->waitfor('/Password: $/i') or die $t->lastline;
$t->print($pass);
print $t->lastline;

$t->waitfor('/TERM= /i') or die $t->lastline;
$t->print($ttyType);
print $t->lastline;

$t->waitfor('/profs*/') or die $t->lastline;
$t->print($profs_id);
print $t->lastline;

$t->waitfor('/SECADMIN/') or die $t->lastline;
$t->print("^[OR");  #problem is here with the disclaimer  you press F3 to get to the prompt
print $t->lastline;

my @output = $t->cmd("setaddr -l | grep 008064");
print $t->lastline;
print @output;
0
Comment
Question by:TristinColby
23 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 150 total points
ID: 18017542
you might try sending  "\eOR"
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18017797
I've tried that.  It doesn't work.  its a ksh, but I don't know if the ksh starts before or after i press f3.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 150 total points
ID: 18017857
Firstly, you need to find what sequence F3 gives you.

From your command line, type

cat -v

then press F3.

It should output something like

^[[13~

which means the sequence F3 sends is

Esc [13~

Which translates to sending

"\e[13~"

0
 
LVL 84

Expert Comment

by:ozo
ID: 18017858
Does it work when you telnet manually?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018116
Tintin,
Its a little weird though. When I press F3 it prints an R then when I press enter it prints ^[OR


I tried sending \e^[OR but it didn't work.  $t->print("\e^[OR");  Here's a list of things I've tried so far.

$t->print("^[OR");
$t->print("\eOR");
$t->print("\e^[OR");
$t->print( "^\[OR";)  
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018120
It does work when I telnet manually.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018188
what do you get when you press F3 in
od -c
can you try a $t-put inseas of $t->print?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018193
Okay , I've figured this much out.  The disclaimer is being displayed by the shell script /u/bin/notices

From the file:
Notices will display any file in the $DD_SYLIB/notices directory.
The notices are displayed as the user logs in before the menu screen.
The find command will delete all files older than 3 days.

end if paste

But what i can't figure out is what triggers the f3 key to let the screen go. here's the script. in its entirety

#!/bin/sh
#@(#)notices    1.2  06/13/90 17:14:12
##
##
##
## Notices will display any file in the $DD_SYLIB/notices directory.
## The notices are displayed as the user logs in before the menu screen.
## The find command will delete all files older than 3 days.
##
##
##

trap "rm -f /tmp/all$$ /tmp/not.$$" 2 9 12

find $DD_SYLIB/notices -type f -mtime +3 -print >/tmp/not.$$

for file in `cat /tmp/not.$$`
do
   if [ -f $file ]
   then
      /bin/rm -f $file
   fi
done


cat $DD_SYLIB/notices/* > /tmp/all$$ 2> /dev/null

if [ $? = 0 ]
then
   viewfile -t"Display Notices" -l2000 /tmp/all$$
fi

rm -f /tmp/all$$ /tmp/not.$$
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018202
I can also do a ctrl-c to get past it but when I send \cc through the script I get the following error:

Uncaught exception from user code:
        pattern match read eof at C:\Documents and Settings\user\Desktop\Fetc
h_All_TC.pl line 58
        Net::Telnet::_croak('Net::Telnet=GLOB(0x1c4dd2c)','pattern match read eo
f') called at C:/Perl/site/lib/Net/Telnet.pm line 539
        Net::Telnet::error('Net::Telnet=GLOB(0x1c4dd2c)','pattern match read eof
') called at C:/Perl/site/lib/Net/Telnet.pm line 362
        Net::Telnet::cmd('Net::Telnet=GLOB(0x1c4dd2c)','setaddr -l | grep 008064
') called at C:\Documents and Settings\user\Desktop\Fetch_All_TC.pl line 58
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018257
Could you try removing the viewfile -t"Display Notices" -l2000 /tmp/all$$
from the script?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018304
Oooh no, I can't do any editing on this file.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:TristinColby
ID: 18018307
Viewfile is the actual application that is asking for f3.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018349
Do you know what the -l2000 means?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018379
lines
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018417
What are the symptoms you are seeing?
Can you get an Input_log and Output_log and compare it t what you see when you telnet manually?

Did you try $t-put with a pause before and after?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018575
yeah, nothing i send bypasses the screen.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018603
What are the symptoms you are getting?  do you die $t->lastline;?
does print @output; print the wrong thing?
How does it compare with what happens manually?
When you telnet manually, can you type F3 before viewfile finishes printing?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18018713
No it doesn't die, it just times out. I've tried a timeout of 100 which is forever. when you hit f3 its almost instantanious.  The file is only the size of a normal dos window.  Its got something to do with the keys i'm sending, but viewfile may interpret f3 different.  
0
 
LVL 84

Expert Comment

by:ozo
ID: 18018769
what times out?  the $t->waitfor('/SECADMIN/')?
Can you print the log of what was sent back?

Can you hit f3 before viewfile starts?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18022536
The timeout issue is fixed.  I mistyped the regex.  no you can't hit f3 before viewfile starts. Its instantanious.  It still won't take the the keys.  You can't do anything until you get past the viewfile application.  No commands.  So the entire script is worthless unless I can get past the viewfile.  Anyone else have any ideas?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 18023274
Do you have a telnet program that can record the exact bytes sent?  If so, use that to log in, press F3, and look at what bytes are sent.

Do you have access to the viewfile program?  Is it a script?  Compiled?  Do  you have the source code?
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18026027
alright, I found out how to bypass it all together. Just enter exit as the termtype and it crashes out the login script so that viewfile doesn't even get executed. ha ha. The only problem is now that i'm not getting any output in my list.  It shows up in my input_log.log just not on the screen, which is what I want. Here's the current script.

use strict;
use warnings;
use diagnostics;

use lib '//server/perl/net/';
use Net::Telnet ();

my $login = "name";
my $pass = "pass";
my $ttyType = "exit";  #Set Term Type to exit to bypass notices
my $profs_id = "creds";

#Begin Telnet

print "Wrong number of arguments" and die "$!\n" unless @ARGV > 0;

my $input = shift;
my $isp = "isp.s0".$input;

my $t = new Net::Telnet (Timeout => 15,
                                     #Prompt => '/\$ $/',
                                     Dump_Log => "Dump_Log.log",
                                     Input_Log => "Input_Log.log");

$t->open($isp);

$t->waitfor('/login: $/i') or die $t->lastline;
$t->print($login);

$t->waitfor('/Password: $/i') or die $t->lastline;
$t->print($pass);

print "Logged in.\n";

$t->waitfor('/TERM=/i') or die $t->lastline;  #Set Term Type to exit to bypass notices
$t->print($ttyType);
print "Term Type Set\n";

$t->waitfor('/profs*/') or die $t->lastline;  #Enter profs id
$t->print($profs_id);
print "profs ID set\n";

print "Getting Thin Client List\n";
#$t->waitfor('m/\/is\/$ $/i');
$t->waitfor('/[\$%#>] $/');

my @output = $t->print("setaddr -l | grep 008064");

$t->waitfor('/[\$%#>] $/');

print @output;   #Right here prints a 1.  Not the actual output.
0
 
LVL 1

Author Comment

by:TristinColby
ID: 18026337
alright guys, its solved.  the issue is with the last 3 lines.  Thanks to everyone who helped.

it SHOULD read

$t->print("setaddr -l | grep 008064");

my @output = $t->waitfor('/[\$%#>] $/');

print @output;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now