• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

Perl pattern recognition

I'm trying to use the Net::Telnet module in Perl.  One of the methods is waitfor().  I'm totally new to perl and can't figure out the syntax for the pattern.  The prompt on the screen that is what I guess the method is waiting for is 'C:\Documents and Settings\tester>'  My question has two parts - 1) is the method waiting for the prompt, or something else? 2) If it's waiting on the prompt, what is the syntax to match that string?

Here's the sample program I'm working with and modifying.  The third waitfor line is the problem.

use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die'); $telnet->open('camel.perlfect.com');
$telnet->waitfor('/login: $/i');
$telnet->print('bilbo');
$telnet->waitfor('/password: $/i');
$telnet->print('baggins');
$telnet->waitfor('/\$ $/i');
$telnet->print('who');
$output = $telnet->waitfor('/\$ $/i');
print $output;

Thanks
0
danborn
Asked:
danborn
1 Solution
 
mdeweerdCommented:
Your third 'waitfor' call is waiting on a prompt that looks like a dollar sign followed by a space ('$ ').
The prompt depends on how the prompt for that user is defined.  You can check it by logging in manually.

Suppose that the prompt is '[/homes/baggins] >' - in that cas you might want to do this:
$telnet->waitfor('/.* > $/i');

That would wait for any line ending in '> '.
You could probably match login prompts by doing this:
$telnet->waitfor('/.* [>\$] ?$/i');

Which matches any line ending in '$' or '>' potentially followed by  a space.

This script does not really take into account any login error that may occur and would
0
 
farzanjCommented:
In Per you match with /pattern/.  For a case insensitive match you use /pattern/i.

The prompt you are seeing is a simple DOS/Windows prompt.  It doesn't look like a telnet prompt and it doesn't look like the program is running.

Did you actually call your program by calling its name on the command prompt?
0
 
danbornAuthor Commented:
Well I am trying to telnet to a Windows machine.  If telnet isn't running, why does it print Welcome to Microsoft Telnet Server?  And I can enter commands like "dir" which show the files in the Windows machine.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
mdeweerdCommented:
If you are contacting a windows host, the prompt likely ends in '>' and is preceded by the path.  The regex that I provided should work unless something else is going on.

If you succeed in doing a telnet manually, there is a compatible service running on the windows machine.
0
 
danbornAuthor Commented:
I clicked submit on the below several hours ago and assumed it would post.  It didn't, because I didn't "enter a description for each file you uploaded.'  Sigh.  Apologies.  

Just tried it and no luck.  I can't attach a perl script but here's the contents of the file I'm trying to run:

#!/usr/bin/perl -w

use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die',Dump_Log=>'dumplog',Input_log=>'inputlog');
$telnet->open('172.16.3.50');
$telnet->waitfor('/login: $/i');
$telnet->print('tester');
$telnet->waitfor('/password: $/i');
$telnet->print('mypassword');
$telnet->waitfor('/.* > $/i');
$telnet->print('who');
$output = $telnet->waitfor('/\$ $/i');
print $output;

It always times out on the "waitfor('/.* > $/i'); " step.  I've attached a screen shot of a successful telnet login using the name and password in the program above.

This script is attempting to login to a virtual PC (running XP) from a virtual linux box running ubuntu if that makes a difference.
telnet-screen-shot.jpg
0
 
ozoCommented:
There does not seem to be a space after the > in the screen shot.
Could you try just waitfor('/>/');
0
 
mdeweerdCommented:
"It always times out on the "waitfor('/.* > $/i');"

In my anwser I indicated a '?' after the space.  That question mark is important as it says that the space is not mandatory.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now