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

I am trying to run a who command on a remote server using Net::Telnet


1. use Net::Telnet; 2. $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die',Input_Log= +>'out.txt',Prompt=>'/.*tstbld.*/'); 3. $telnet->open('seahawk'); 4. $telnet->login('tstbld', 'vilc0n'); 5. $telnet->cmd(String=>'cd /builds/htpc2003r1/base/menu', Prompt=>'/. +*htpc.*/'); 6. $prompt = '/pkmsqa/'; 7. print $telnet->cmd(String=>'who', Prompt=>$prompt);


When I see the actual output from line 7 in out.txt file, it shows all lines returned by who command. Basically the command prompt is returned twice, once before the results and once after the results

[/builds/htpc2003r1/base/menu] seahawk:>who pkmsqa pts/0 Nov 07 03:19 (10.80.12.176) tstbld pts/3 Nov 07 08:40 (10.80.12.176) tstbld pts/4 Nov 08 02:35 (10.80.12.154) pkmsqa pts/5 Nov 08 04:59 (10.80.12.154) [/builds/htpc2003r1/base/menu] seahawk:>

In line 6 if I specify prompt = ‘/pkmsqa/', it returns everything before pkmsqa
[/builds/htpc2003r1/base/menu] seahawk:>who

If I specify prompt = ‘/>/', it returns everything before the first occurrence of >

[/builds/htpc2003r1/base/menu] seahawk:

How do I specify prompt so that it returns me everything before the second > I tried /Z option, $& option, >& option but nothing works. The match is always being done with first occurrence and not the last occurrence.

Thanks, KK

Replies are listed 'Best First'.
Re: Matching prompt in Net::Telnet
by wjw (Priest) on Nov 08, 2006 at 21:45 UTC
    Ok, last shot at this :-)

    use Net::Telnet; $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die',Input_Log=>'o +ut.txt',Prompt=>'/.*tstbld.*/'); $telnet->open('seahawk'); $telnet->login('tstbld', 'vilc0n'); $telnet->cmd(String=>'cd /builds/htpc2003r1/base/menu', Prompt=>'/.*ht +pc.*/'); $prompt = '/seahawk\:\>/'; @ret= $telnet->cmd(String=>'who', Prompt=>$prompt); print @ret;

    from perl doc....

    In a list context, just the output generated by the command is returned, one line per element. In other words, all the characters in between the echoed back command string and the prompt are returned. If the command happens to return no output, a list containing one element, the empty string is returned. This is so the list will indicate true in a boolean context. On time-out, eof, or other failures, the error mode action is performed.

    Sorry I can't be of more help.. :-)

    ...the majority is always wrong, and always the last to know about it...

      I've tried that before :) Didn't work. But thanks a lot anyway. You did your best :)
Re: Matching prompt in Net::Telnet
by wjw (Priest) on Nov 08, 2006 at 20:10 UTC
    It has been a long time since I used Net::Telnet, so excuse me if I am way off base here:

    $prompt= '/\nseahawk\:\>$/'

    Just can't recall whether you can hand in a regexp ... . Just a thought :-)

    ...the majority is always wrong, and always the last to know about it...

      Somehow that doesn't work. If I specify
      $prompt= '/\nseahawk\:\>$/'
      it returns
      [/builds/htpc2003r1/base/menu]
      and what's surprising is that even the log is missing the output from who command
      [/builds/htpc2003r1/base/menu] seahawk:>
        Sure! Like an idiot I missed the fact that the regex would catch at the top before the 'who', though maybe adding a return .... . Like this?: $prompt= '/\nseahawk\:\>\n$/'

        Wish I knew what was at the end of that last prompt. If there is a '\n' there, this should work. Give it a try...

        ...the majority is always wrong, and always the last to know about it...

Re: Matching prompt in Net::Telnet
by runrig (Abbot) on Nov 08, 2006 at 22:31 UTC
    Have you tried debugging with the Input_Log and Dump_Log options? It seems like you should be able to just set the Prompt once in the call to new() (to something like /:(> )?$/) and then leave out the Prompt option from the commands.
Re: Matching prompt in Net::Telnet
by quester (Vicar) on Nov 09, 2006 at 05:23 UTC
    I believe the real problem is merely that the value specified for prompt in line 5 is different than the value in line 7. If you use the same value in each case, you won't have the thorny problem of seeing a prompt both before and after the command in line 7 is sent.
      Thanks everyone for your valuable input. Quester, I guess what you said was right. I removed all prompts and just put in one prompt at the begining of object creation.
      use Net::Telnet; $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die',Input_Log=>'out.txt', Prompt=>'/[\$%#>]/');
      This works!! The default prompt  '/[\$%#>] $/' didn't work because it had the extra $ towards the end of the string.
Re: Matching prompt in Net::Telnet
by laceytech (Pilgrim) on Nov 09, 2006 at 18:00 UTC
    You could capture the entire result and then grep the result for the value you need to find.
    my @result = $t->cmd('who'); if (grep /pkmsqa/, @result){ .... }