Solved

Perl pattern recognition

Posted on 2012-04-13
7
602 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File.WriteAllLines problem at random C# ASP.NET 6 64
endX challenge 2 63
What is Python programming? 3 102
Currency Conversion? 1 68
This is an explanation of a simple data model to help parse a JSON feed
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

929 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

21 Experts available now in Live!

Get 1:1 Help Now