I am having difficulty with integrating the Net::Telnet with
my prompt. I am developing some Perl code to log into an account
with Net::Telnet and have it execute several commands, but am
having trouble making the regular
expression that searches for different shell prompt types work for
a wide variety of different prompts
(this is contained in the 'Prompt' variable of the Net:Telnet module).
My code to initiate the login and execute a command looks like this
(this code is just for testing purposes, so forgive the failure to
follow various Perl conventions):
#!/usr/bin/perl -w
use strict;
my $hostname="localhost";
my $port=23;
#my $username="testuser1";
#my $password="1helpnow";
#my $username="testuser2";
#my $password="1please";
my $username="testuser3";
my $password="1password";
use Net::Telnet();
my $t = new Net::Telnet (Timeout=>10,
Prompt=> '/[\$%#>\d\w\r] $/');
$t->dump_log("log.telnet.dat");
$t->open(Host => $hostname, Port => $port);
$t->login($username, $password);
my @lines = $t->cmd("who");
$t->lastline;
$t->close;
print "@lines\n";
My problem is that for testuser1 and testuser3, I was not able
to log in while, testuser2 logs in beautifully. I have isolated
the problem and have determined that it lies in each user's .bashrc
file, in particular, the PS1 line in the file.
The .bashrc file for testuser1 and testuser3 were exactly the same.
The PS1 file for those two users looks like this:
(Hang with me here... this really is a Perl question)
RED="\[\033[1;31m\]" # Color Definitions for bash
LIGHT_RED="\[\033[1;31m\]"
BLUE="\[\033[1;34m\]"
WHITE="\[\033[1;37m\]"
NC="\[\033[0;0m\]" # No Color
PS1="${TITLEBAR}\
$BLUE[$RED\$(date +%I:%M)$BLUE]\
$BLUE[$LIGHT_RED\u@\h:\w$BLUE]$WHITE#$NC "
When I remove the $NC from the end of the PS1 prompt, I am
able to log in successfully with Net::Telnet. However,
since there are users who may be using this .bashrc file, I
have to support it with my perl script. Therefore, I must
create a regular expression for the Net::Telnet 'Prompt' variable
that will treat the $NC (or any other strange character sequence that
someone might use for a shell prompt) in the same way that it does
for a # $ % or >.
I have read carefully through the Net::Telnet documentation, but have
had no luck finding much that is helpful. I have also tried
using the regular expressions found in
reg ex and the prompt but have
had no luck. If anyone has had any experience with Net::Telnet
and has any suggestions regarding this topic, I would be very
grateful to hear what you have to say. Thank you for reading this.