LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2012, 10:32 PM   #1
kenji
Member
 
Registered: Sep 2004
Location: Philippines
Distribution: Slackware
Posts: 143

Rep: Reputation: 16
Perl/Expect script connecting to a server using telnet.


Hi LQ,

I have written a perl/expect script to connect in our servers via telnet. The problem is I cannot connect because I am having problem finding out the regex of the prompt. I have search and tried what I found in google and here in LQ but still having problems. Below is my script and the error message that I am having.

The prompt is "hostname:username[/home/username]" (without the quotes)

--- perl script ---

username@hostname:~/work/scripts$ cat telnet.pl
#!/usr/bin/perl

use strict;
use warnings;

my $username='username';
my $passwd='password123!@#';
my $t;
my @lines;

use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
#Prompt => qr/hostname:username[/home/username]/);
#Prompt => "hostname:username[/home/username]");
Prompt => 'hostname:username[/home/username]');
$t->open("192.168.0.5");
$t->login($username, $passwd);
@lines = $t->cmd("who");
print @lines;
$t->close;

# hostname:username[/home/username]
username@hostname:~/work/scripts$ perl telnet.pl
ignoring bad Prompt argument "hostname:username[/home/username]": missing opening delimiter of match operator at telnet.pl line 12

--- Error message ---

Unmatched [ in regex; marked by <-- HERE in m/hostname:username[ <-- HERE / at telnet.pl line 13.
ignoring bad Prompt argument "hostname:username[/home/username]": missing opening delimiter of match operator at telnet.pl line 12

--- expect script ---

#!/usr/bin/expect
spawn telnet hostname
expect {login:}
send "username\n"
expect {Password:}
send "password123!@#\n"
send "cd /home/username/\n"
send "ls -ltr"

I also have tried the perl code below.

Prompt => 'hostname:username[/home/username]');
Prompt => "hostname\:username\[\/home\/username\]");

Thank you in advance for your help.
 
Old 08-31-2012, 01:53 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
$t = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/');
$t->open("sparky");
$t->login($username, $passwd);
@lines = $t->cmd("who");
print @lines;

In the documentation it is like this...
 
Old 08-31-2012, 02:29 AM   #3
kenji
Member
 
Registered: Sep 2004
Location: Philippines
Distribution: Slackware
Posts: 143

Original Poster
Rep: Reputation: 16
Hi LQ,

Thanks j-ray for your comment. That is the first code that I tested.

I found out the solution. Below is the code that works for me. I forgot that single quotes and double quotes differ on scripting languages. I will also try this using expect.

Prompt => '/hostname\:username\[\/home\/username\]/');

Last edited by kenji; 08-31-2012 at 02:30 AM. Reason: To clarify the message :D
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
expect script to telnet into a router m4rtin Programming 5 08-02-2010 08:03 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Telnet script on Unix machine without expect cristoph_ Programming 7 12-24-2008 10:23 AM
Expect Scripting:- script not coming out of telnet session. nik1984 Programming 2 09-11-2008 08:14 AM
telnet server not reliable for expect script powah Linux - Software 0 11-16-2005 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration