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.
|