Good afternoon,
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.

In reply to Net::Telnet stalls by JoeJaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.