Solved

Perl pattern recognition

Posted on 2012-04-13
7
616 Views
Last Modified: 2013-11-13
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
Comment
Question by:danborn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:mdeweerd
ID: 37844973
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37845128
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
 

Author Comment

by:danborn
ID: 37845156
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:mdeweerd
ID: 37845229
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
 

Author Comment

by:danborn
ID: 37845352
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
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 37845583
There does not seem to be a space after the > in the screen shot.
Could you try just waitfor('/>/');
0
 
LVL 2

Expert Comment

by:mdeweerd
ID: 37845836
"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

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Simple Linear Regression
Introduction to Processes

688 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