Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl-> Telnet problem.. HELP PLS

Status
Not open for further replies.

geffry

Programmer
Jun 26, 2002
33
US
Hi,

I have a script which logins to many unix stations and executes a command and gets the output.

In some of the unix stations,it is not able to get the prompt after login and it times out.

I found out that the problem is due to,

login: xyz
Password:
Last login: Wed Apr 20 10:06:28 from arc54
10:09am up 112 day(s), 23:35, 7 users, load average: 0.14, 0.09, 0.06
arc55%


The problem is due to the the line appearing after the Last login info,ie the up time and users info.. The script is able to login to the machines where this info is not available.

Can any one let me know,how to make the script fail-proof. ie , make the script to waitfor the command prompt.


#!/usr/bin/perl

use lib '/usr/local/lib/perl5/site_perl/5.005/';
use Net::Telnet;
$telnet = new Net::Telnet( Timeout=>10,Errmode=>'die',Prompt=>'/[$%>#] $/i',Dump
_Log=>'/tmp/fail.log',Input_Log=>'/tmp/pass.log');
$telnet->open('arc55');
$telnet->login('tera','tera1');
$telnet->waitfor('arc55%');
sleep 5;
@outp = $telnet->cmd('/usr/sbin/psrinfo -v');
print @outp;




 
What is this line for? Isn't this path inside your @INC?
use lib '/usr/local/lib/perl5/site_perl/5.005/';

Are you sure it times out? Maybe it dies somewhere else, but if you dont put something like
'or die " failed to do bla bla: $!"; '
you will never know. Also try to use in all of your scripts 'use strict' and 'use warnings'.

Try to increase the Timeout option to '30'

Maybe because of, many users are logged in at the same time, it takes a little more time than 10 seconds for the *nix machine to handle the telnet request of yours


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Increasing the timeout sounds like a good first step.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Thanks for ur replies.

I did take dump_log and input_log, it has logged into system, but it is not able to wait for the prompt, as these machines on login shows the uptimes and no. of users before the command prompt.

Thats why the script passes on some machines and fails on other.


login: tera
Password:
Last login: Wed Apr 20 10:14:02 from arc54.tera
1:19pm up 113 day(s), 2:46, 7 users, load average: 0.01, 0.01, 0.02
sparc55%



IT is becos of this line,
1:19pm up 113 day(s), 2:46, 7 users, load average: 0.01, 0.01, 0.02

the script gets timed out , on not getting the prompt.

For the machines which does not show this info, the script works.
 
I understood what you meant.

I don't know why it works on the machines without the uptime command output - From the example in the documentation I don't think it should work on any of them....

Here's the example from the documentation that I mean:

use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/');
$t->open("sparky");
$t->login($username, $passwd);
@lines = $t->cmd("who");
print @lines;

I think you should be specifying arc55 in the Prompt bit when you create the Telnet option using new.

Try this:

use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/arc55%\s*$/');
$t->open("sparky");
$t->login($username, $passwd);
@lines = $t->cmd("who");
print @lines;

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hi Mike,

It is surely bcos of the uptime it fails, i dont know how to make my script work when there is uptime displayed @ Login.

here is my latest script

#!/usr/bin/perl

use lib '/usr/local/lib/perl5/site_perl/5.005/';
use Net::Telnet;
$telnet = new Net::Telnet( Timeout=>10,Errmode=>'die',Prompt=>'/arc55% $/i',Du
mp_Log=>'/tmp/fail.log',Input_Log=>'/tmp/pass.log');
$telnet->open('arc55');
$telnet->login('tera','tera1');
@outp = $telnet->cmd('/usr/sbin/psrinfo -v');
print @outp;

I also tried ur setting for the prompt it did not work.

Error msg:
timed-out waiting for command prompt at ./test.pl line 7

sparc54% more pass.log


SunOS 5.8

login: tera
Password:
Last login: Wed Apr 20 13:55:58 from arc54
1:56pm up 113 day(s), 3:22, 5 users, load average
: 0.19, 0.11, 0.09
sparc55%

=================

Thanks in Advance



 
Then try

Prompt => '/sparc55%\s*$/');

The prompt setting is the key to your problem I am sure - go see if there's a debug setting in Net::Telnet that you can use to see what the module is up to.


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
The section on debugging in the documentation says
The typical usage bug causes a time-out error because you've made incorrect assumptions about what the remote side actually sends. The easiest way to reconcile what the remote side sends with your expectations is to use input_log() or dump_log().

dump_log() allows you to see the data being sent from the remote side before any translation is done, while input_log() shows you the results after translation. The translation includes converting end of line characters, removing and responding to TELNET protocol commands in the data stream.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I had that log enabled.. i can see that it logs in the machine.. but with the result of uptime command, the waitfor command prompt gets screwed up.

so it gets timed out...

Any one who have runup with this problem.

"when the machine shows the result of uptime command on login, the telnet->login command times out waiting for the command prompt
 
Hi,

hmmm if it is not the prompt then it might be that the command you are giving has a long output and wants you to press more.

If that is the case then @output will never get filled I believe and won't print.

Could you change your code to the following and tell us what happens?

Code:
#!/usr/bin/perl

use lib '/usr/local/lib/perl5/site_perl/5.005/';
use Net::Telnet;

#create new telnet object and set prompt, timeout a.s.o.

$telnet = new Net::Telnet( Timeout=>10,Errmode=>'die',Prompt=>'/[$%>#] $/i',Dump
_Log=>'/tmp/fail.log',Input_Log=>'/tmp/pass.log');

#pipe everything to stdout so you see everything happening live.

[b]$telnet->input_log(\*STDOUT);[/b]

#open telnet connection to arc55

$telnet->open('arc55');

#login to the switch and wait for the prompt

$telnet->login('tera','tera1');

#run the command and wait for the prompt.

@outp = $telnet->cmd('/usr/sbin/psrinfo -v');

#print the received output of the command to screen.

print @outp;

"when the machine shows the result of uptime command on login, the telnet->login command times out waiting for the command prompt"

After the login command there should not be a waitfor. The $telnet->login command (just like the $telnet->cmd()) makes use of the prompt given in $telnet = new Net::Telnet()

InDenial

 
I think the problem, is with the telnet->login command is not able to get the prompt to match with i gave.. This due to the uptime command info being present and the telnet.pm module does not translate it properly. I have no clue to solve the problem.

Even if i gave a one liner command output,it does not matter as the script times out in the telnet->login ..line itself.

btw,i pasted the code u sent, i get this error,

Can't call method "autoflush" on unblessed reference at /usr/local/lib/perl5/site_perl/5.005//Net/Telnet.pm line 2001


The prompt i use now is Prompt => /arc55% $/

How do i give the prompt,to match irrespective of the uptime command info coming during the login.

this is the dump_log

< 0x00000: 4c 61 73 74 20 6c 6f 67 69 6e 3a 20 57 65 64 20 Last login: Wed
< 0x00010: 41 70 72 20 32 30 20 31 34 3a 33 31 3a 31 34 20 Apr 20 14:31:14
< 0x00020: 66 72 6f 6d 20 73 70 61 72 63 35 34 2e 73 61 6e from arc54

< 0x00000: 1b 5d 32 3b 73 70 61 72 63 35 35 3a 2f 73 70 61 .]2;arc55:/spa
< 0x00010: 63 65 2f 53 61 6e 74 65 72 61 07 ce/tera.

< 0x00000: 20 20 31 3a 30 33 70 6d 20 20 75 70 20 31 31 34 1:03pm up 114
< 0x00010: 20 64 61 79 28 73 29 2c 20 20 32 3a 33 30 2c 20 day(s). 2:30.
< 0x00020: 20 35 20 75 73 65 72 73 2c 20 20 6c 6f 61 64 20 5 users. load
< 0x00030: 61 76 65 72 61 67 65 3a 20 30 2e 30 37 2c 20 30 average: 0.07. 0
< 0x00040: 2e 30 35 2c 20 30 2e 30 35 0d 0a 1b 5d 32 3b 2f .05. 0.05...]2;/
< 0x00050: 73 70 61 63 65 2f 53 61 6e 74 65 72 61 07 1b 5d space/tera..]
< 0x00060: 31 3b 73 70 61 72 63 35 35 07 0d 00 1b 5b 6d 1b 1;arc55....[m.
< 0x00070: 5b 6d 1b 5b 6d 1b 5b 4a 73 70 61 72 63 35 35 25 [m.[m.[Jarc55%
< 0x00080: 20 1b 5b 4b .[K

 
Seeing your dump_log it looks like the machine is echoing something back

< 0x00080: 20 1b 5b 4b .[K

What happens if you make your prompt

Prompt => /arc55%/

instead of

Prompt => /arc55% $/

does it get any further?... I think that the echo you get from the sparc has some unexpected characters in it like a newline.

Also... I searched the internet a bit and found a link to a newsgroup posting wich might apply to you.

Link

InDenial

 
Hi InDenial,

I really appreciate your help.

I tried replacing the prompt and it works now,but the problem is it fails on the machines which does not have uptime info displayed on login (these machines worked bfore). :)



 
Are those machines exactly the same except for the uptime info? If so this might explain what is in the link I gave you. only this machines echo's something back during login which the telnet script does not know how to handle.

Like in the newsgroup posting I gave you you could try to use the cmd_remove_mode.

Code:
#!/usr/bin/perl

use lib '/usr/local/lib/perl5/site_perl/5.005/';
use Net::Telnet;
$telnet = new Net::Telnet( Timeout=>10,
             Errmode=>'die',
             Prompt=>'/[$%>#] $/i',
             Dump_Log=>'/tmp/fail.log',
             Input_Log=>'/tmp/pass.log',
             [b]cmd_remove_mode => 1[/b]);

$telnet->open('arc55');
$telnet->login('tera','tera1');
@outp = $telnet->cmd('/usr/sbin/psrinfo -v');
print @outp;

if the above does not work you could try to do your own login using waitfor.

another question... why is that use lib in there? might be that you are using an old telnet module. Or is that the only place the telnet module resides?

Time for bed now so.... will check back tomorrow...

InDenial

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top