Solved

Perl pattern recognition

Posted on 2012-04-13
7
600 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
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This article will show, step by step, how to integrate R code into a R Sweave document
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 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

23 Experts available now in Live!

Get 1:1 Help Now