Solved

Perl pattern recognition

Posted on 2012-04-13
7
604 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i compile this github project?? 2 85
index Out OF Range Exception error 4 63
C# application error "Parameter Is Not Valid". 3 98
SQL Insert parts by customer 12 35
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.

832 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