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

Hi Monks,

I am writing script automate run telnet command and return result by Net::telnet. I found some of server have different bash prompt , something is end with # and some end with > or else. How can I create a standard bash prompt?

And I found when the server is prompt menu, it cannot run the command I request...what can I do rather than modify .profile?

Thanks a lot

Replies are listed 'Best First'.
Re: Net::telnet question
by Fletch (Bishop) on Apr 22, 2008 at 13:07 UTC

    One of the first commands you should do is to set PS1 to your own fixed value. That way you know what to expect rather than having to deal with whatever the other side's sending back.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      yes, but my company has so many restriction on development... so what I can is "adapt" to their environment, but I can't change it.. ~o~(cry)

        You misunderstand; I'm not saying change the default environment, I'm saying send a command to set the transient PS1 in your running shell to something fixed. The default Net::Telnet value of qr/[\$%#>] $/ should be good enough to get you to where you can send something (and if it's not then build on it until it is), then set your fixed prompt as the first command.

        $tel->cmd( "PS1='fixed> '; export PS1" ); $tel->prompt( qr/fixed> $/ );

        That setting is not going to persist past the one process you're talking to, but it allows you to look for a known marker in your sessions (and/or dynamically tweak the prompt if you ever might mistake output data for the prompt).

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Net::telnet question
by lwicks (Friar) on Apr 22, 2008 at 12:54 UTC
    You can use something like:
    $t->waitfor( Match => '/FLMON>/', Timeout => 120 );
    to look for the prompt. This is particularly handy if the prompt changes through the process.

    Else if the prompt remains static, you can set the prompt permanently like this:

    $t = new Net::Telnet (Timeout => 10, Prompt => '/bash\$ $/');

    Obviously, http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm is place to look for more details.

    Hope that helps.
    Lance

    Kia Kaha, Kia Toa, Kia Manawanui!
    Be Strong, Be Brave, Be perservering!

Re: Net::telnet question
by benlaw (Scribe) on Apr 22, 2008 at 14:20 UTC
    There may some Updated from my question, thanks monks

    May be I need describ more detail on menu mode..

    in .profile >>

    stty -isig stty ignbrk set -o ignoreeof trap "" INT trap "" stop trap "" quit . $HOME/menu.sh
    in $HOME/menu.sh
    echo '\t +---------------------------------------------------------+ +' echo '\t | Operation Menu |' echo '\t | ======================================================= | +' echo '\t | | +' ..... ...
    I try to use output log and Dump log see more information: DUMPLOG:
    ....login successfully .... < 0x00460: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2 +d 2d 0d 0a 4c 61 73 -----------..Las < 0x00470: 74 20 75 6e 73 75 63 63 65 73 73 66 75 6c 20 6c t unsuc +cessful l < 0x00480: 6f 67 69 6e 3a 20 54 68 75 20 41 70 72 20 31 37 ogin: T +hu Apr 17 < 0x00490: 20 31 30 3a 35 33 3a 31 33 20 54 41 49 53 54 20 10:53: +13 TAIST < 0x004a0: 32 30 30 38 20 6f 6e 20 2f 64 65 76 2f 70 74 73 2008 on + /dev/pts < 0x004b0: 2f 30 20 66 72 6f 6d 20 31 30 2e 31 35 39 2e 36 /0 from + 10.159.6 < 0x004c0: 36 2e 35 30 0d 0a 4c 61 73 74 20 6c 6f 67 69 6e 6.50..L +ast login < 0x004d0: 3a 20 54 75 65 20 41 70 72 20 32 32 20 31 39 3a : Tue A +pr 22 19: < 0x004e0: 31 32 3a 30 36 20 54 41 49 53 54 20 32 30 30 38 12:06 T +AIST 2008 < 0x004f0: 20 6f 6e 20 2f 64 65 76 2f 70 74 73 2f 33 20 66 on /de +v/pts/3 f < 0x00500: 72 6f 6d 20 31 30 2e 31 35 39 2e 36 37 2e 32 34 rom 10. +159.67.24 < 0x00510: 0d 0a 0d 0a .... > 0x00000: 64 66 0d 0a df..
    Output_log :
    user1 pAssword123 df
    print out after script finish

    ---------------------------------------------------------------------- +----------
    The script I almost copy from cpan
    use Net::Telnet (); $username = "user1"; $passwd = "pAssword123"; $prompta = '/\w.*/'; $t = new Net::Telnet (Prompt => $prompta, Timeout => 20, Dump_Log => "log.txt", Output_log=>"outlog.txt" ); $t->open("10.188.37.10"); $t->login($username, $passwd); @lines = $t->cmd("df"); print @lines;