HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

Perl telnet VMS problem

 
SOLVED
Go to solution
sanshi_leilei
Occasional Visitor

Perl telnet VMS problem

Morning.
I want to telnet OpenVMS system by Perl and run a command. I can log in success but cannot identify the input. Below is my script:

use Net::Telnet();
use Test::Harness::Straps;
my ($hostname, $username, $password, $t,@lines);
$username="name";
$password="password";
$hostname= "a.b.c.d";
my $strap = Test::Harness::Straps->new();
$t = new Net::Telnet (Timeout=>20);
$t->open (Host =>$hostname);
$t->waitfor(-match=> '/Username: ?$/i',
-errmode =>"return") or die "Username problem connecting to host:", $t->lastline;
$t->print ($username);
$t->waitfor(-match=> '/Password: ?$/i',
-errmode =>"return") or die "Password problem connecting to host:", $t->lastline;
$t->print ($password);
sleep (5);
$t->waitfor(-match=> '^hostname\s.*> ?$/i',
-errmode =>"return") or die "Wait time is not enought long:", $t->lastline;
$t->print("copy xlbyperl.txt xlbyperl1.txt");


And the run result is: Wait time is not enought long:", "name".
8 REPLIES
Craig A Berry
Honored Contributor

Re: Perl telnet VMS problem

It doesn't look as though your problem is with either Perl or telnet. Most likely it's because this particular script is expecting the command prompt to match the pattern '^hostname\s.*> ?$/i' and chances are your system's command prompt doesn't match that pattern. Change your command prompt or change your program to look for the prompt you actually have. (Perhaps you meant to use the variable $hostname and not the string 'hostname'?)
Hein van den Heuvel
Honored Contributor

Re: Perl telnet VMS problem

Sanshi had send this question to me by Email a while back.

Here is what I asked in return, but I haven;t seen answers...

Just casually reading is seems to me that it is waiting for a very specific prompt:
"^hostname\s.*>"
So that is a word 'host' at the begin of a line followed by any, or no, white-space, and a '>' character.
When you log in manually, is that what you see?
What do you see as prompt? Just a "$ " perhaps?
Does the word 'hostname' have to be edited to match reality?
Does it need to be a variable ($hostname), or something like '\w+' to match any word?

Finally where is that perl running?
On the OpenVMS box itself?
Did you try from windows/linux?

Regards,

Hein
Jeremy Begg
Trusted Contributor

Re: Perl telnet VMS problem

The result being seen is that it's waiting on "name" i.e. the Username prompt. It looks to me like it's waiting for "Username: ?" i.e. with a trailing '?'. But when VMS prompts the user at login there's no '?', hence the Perl script stalls.

(Of course, it may be that the trailing '?' is required by the waitfor() method, but I'd be surprised if that was the case.)

Regards,
Jeremy Begg
Craig A Berry
Honored Contributor

Re: Perl telnet VMS problem

>The result being seen is that it's waiting on "name" i.e. the
>Username prompt. It looks to me like it's waiting for
>"Username: ?" i.e. with a trailing '?'. But when VMS prompts
>the user at login there's no '?', hence the Perl script stalls.

The match in the waitfor method is done on a regular expression and the question mark means the preceding character (or character class, or grouping) is optional. If it died waiting for username I think it would die with the username error, not the prompt error.

The docs for the module (including how to turn on debugging options) are here:

http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm


sanshi_leilei
Occasional Visitor

Re: Perl telnet VMS problem

Thank you all so much. I'm trying the script on windows to remote access OPENVMS.
I tried to make the prompt simple and tried again. It still does not work. And the problem is: My script can only see the line before last line(the last line is what I want to input my command in). It cannot see the last line. And does anyone was experienced in telnet openvms? Could you post your success script for me? Appreciate it very much!

sanshi leilei
Hein van den Heuvel
Honored Contributor

Re: Perl telnet VMS problem

Sanshi my friend, you are NOT helping.
Think about it for a moment, what would we need to know?

1) What does a normal telnet sequence look like on the target box? Just execute one manually all the way including the first shell line (DCL prompt)
For example, does it look like:


N O T I C E

Access is for subscribed individuals only.
Username: hein
Password:
Welcome to OpenVMS (TM) Alpha Operating System, Version V8.3 on node EISNER
Last interactive login on Monday, 2-AUG-2010 05:58:43.15
Last non-interactive login on Wednesday, 28-JUL-2010 09:13:02.07
1 failure since last successful login

$


2) Which 'die' command is executed? Username, Password, or 'Wait time'? I believe the latter.

3) Is there an error being returned? print $t->errmsg ?


Did you google and read? My first hits:

-- http://www.foo.be/docs/tpj/issues/vol2_4/tpj0204-0009.html

-- http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

Why the "?" in the username and password match strings? It shouldn't hurt, but still. You should know what to expect.

Good luck!
Hein
Craig A Berry
Honored Contributor
Solution

Re: Perl telnet VMS problem

There were several easily fixable problems in the original script. Hein mentioned some of these, and since most of his advice seems to have been ignored, I expect mine will be too, but here goes:

1.) As I mentioned earlier, the documentation clearly describes how to turn on debugging by adding the "Dump_log" parameter when calling the "new" method. There is absolutely no excuse for guessing (or leaving potential respondents to guess) how far it got before failing.

2.) Reporting the value of the "lastline" method on error is really not much help. That will show you the last thing successfully sent and nothing at all about what went wrong. Using the "errmsg" method does work if what you want it is the error message.

3.) The "sleep (5);" line just makes the client stall while the server is trying to write data to it. Deleting it and letting the waitfor method do what its name implies works better.

4.) The prompt that it's expecting is definitely not the default, and while possible, is not like anything I've seen on a VMS system. If the following command were in sys$manager:sylogin.com or the login.com of the user running this, it would create such a prompt:

$ set prompt="hostname ''f$getsyi(""NODENAME"")'> "

but I can't think of a reason to change the prompt -- just make the script able to catch whatever prompt(s) it will be run against.

I've attached a script that corrects the problems above. It sends the SHOW TIME command to the host and looks like so when run on my system with the default dollar sign prompt:

% perl nettelnet.pl myhost.mydomain.com
4-AUG-2010 19:19:49
Craig A Berry
Honored Contributor

Re: Perl telnet VMS problem

Hmm. Trying the attachment again. Also inline in case it fails again:


use strict;
use Net::Telnet();

my $username = shift @ARGV;
my $password = shift @ARGV;
my $hostname = shift @ARGV;

my $t = new Net::Telnet(Timeout=>20,
Dump_log => 'nettelnet.log');
$t->open(Host => $hostname);

$t->waitfor(-match => '/Username: ?$/i',
Errmode => 'return')
or die "Username problem connecting to host:", $t->errmsg;
$t->print ($username);

$t->waitfor(-match => '/Password: ?$/i',
Errmode => 'return')
or die "Password problem connecting to host:", $t->errmsg;
$t->print ($password);

$t->waitfor(-match => '/[\$%#>] $/',
Errmode => 'return')
or die "Timeout waiting for prompt:", $t->errmsg;

print $t->cmd("show time");