Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Telnet using Expect module

by gaurav (Sexton)
on Aug 26, 2013 at 06:19 UTC ( [id://1050902]=perlquestion: print w/replies, xml ) Need Help??

gaurav has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,I have been facing a problem since last day ,but unable to get resolved it

It is my code

my $exp = Expect->spawn("telnet") or die "Cannot spawn telnet: $!\n";; my ($username,$password,$timeout); $username = 'ackerman'; $password = 1454; $timeout = 1; print localtime(time)."\n"; my $spawn_ok; $exp->expect($timeout, [ qr'login: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$username\n"); exp_continue; } ], [ 'Password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn telnet.\n"; } } ], [ timeout => sub { die "No login.\n"; } ], '-re', qr'[#>:] $', #' wait for shell prompt, then exit + expect );

And here is the output

[root@localhost Perl_Ping]# perl Mon Aug 26 11:48:21 2013 Trying Connected to Escape character is '^]'. User name: [root@localhost Perl_Ping]#

It is getting exit without taking username and password,don't know what's wrong ?I hope that you people will guide me through this

Replies are listed 'Best First'.
Re: Telnet using Expect module
by McDarren (Abbot) on Aug 26, 2013 at 07:57 UTC
    This does not directly answer your question... but

    From the Expect documentation:

    "Are you sure there is no other, easier way? As a rule of thumb, Expect is useful for automating things that expect to talk to a human, where no formal standard applies. For other tasks that do follow a well-defined protocol, there are often better-suited modules that already can handle those protocols. Don't try to do HTTP requests by spawning telnet to port 80, use LWP instead. To automate FTP, take a look at Net::FTP or ncftp ( You don't use a screwdriver to hammer in your nails either, or do you?"

    In this case, you have a well-defined protocol, and the better-suited module would be Net::Telnet. Have you considered using that instead?

      Even better would be to connect to the remote host through SSH if possible.
Re: Telnet using Expect module
by slavik (Initiate) on Aug 26, 2013 at 07:54 UTC
    By the looks of it, you are expecting 'login:' but you are getting 'User name:'

      Thank you very much

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1050902]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-14 21:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found