To remove trailing prompt from the output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thamayanthi
    New Member
    • Feb 2008
    • 8

    To remove trailing prompt from the output

    Iam trying to connect to the remote Linux machine from the Windows box using the Perl module use Net::Telnet.The following is the code,

    [CODE=perl]use Net::Telnet;
    $telnet = new Net::Telnet (Timeout=>10,Er rmode=>'die');
    $telnet->open('hostname ');
    $telnet->waitfor('/login: $/i');
    $telnet->print('usernam e');
    $telnet->waitfor('/password for tsom31: $/i');
    $telnet->print('passwor d');

    @r=$telnet->cmd('who');
    print @r;[/CODE]
    It gives the output as shown below,

    Code:
    who
    login: Resource temporarily unavailable while getting initial credentials
    Last login: Thu Mar  6 10:20:30 from 192.168.*.*
    [tsom31@tsom31 ~]$ who
    root     :0           Mar  4 07:35
    root     pts/3        Mar  6 07:46 (192.168.*.*)
    tsom31   pts/4        Mar  6 10:21 (192.168.*.*)
    root     pts/2        Mar  6 07:29 (:0.0)
    [tsom31@tsom31 ~]
    From the output i should remove the Last Prompt
    for ex:[tsom31@tsom31 ~]

    please some one help me in this

    Thanks in advance........ ..

    Regard's
    Thamayanthi
    Last edited by eWish; Mar 6 '08, 07:41 AM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    If you want to remove the last line in the output unconditionally , remove the last element of the array before printing it.
    Code:
    pop @r;
    print @r;
    If you want to remove last line only if it is a prompt,use:
    Code:
    pop @r if($r[$#r]=~/^\[.*\]$/);
    print @r;

    Comment

    • thamayanthi
      New Member
      • Feb 2008
      • 8

      #3
      Thank for your reply........

      It's a wonderful solution!!!!

      I have tried that command and it is working fine.

      one more doubt i have is how to remove the first few lines of the same output
      for ex:
      who
      login: Resource temporarily unavailable while getting initial credentials
      Last login: Thu Mar 6 12:09:51 from 192.168.70.71
      [tsom31@tsom31 ~]$ who

      this need to be removed while printing.

      please suggest me if you have any idea about this........... ...

      Regard's,
      Thamayanthi

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Use shift to remove elements from begining of array. I am again assuming you want to remove lines from begining upto a line where you reach a prompt.
        Code:
        my $i=0;
        shift @r until($r[$i++]=~/^\[/); ## delete lines upto prompt
        shift @r; # delete line with prompt
        pop @r if($r[$#r]=~/^\[.*\]$/);
        print @r;

        Comment

        • thamayanthi
          New Member
          • Feb 2008
          • 8

          #5
          Hi,

          If i add the above code in the program it is taking long time to execute..I didn't get any output.

          please help me in this.........



          Regard's
          Thamayanthi.

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by thamayanthi
            Hi,

            If i add the above code in the program it is taking long time to execute..I didn't get any output.

            please help me in this.........



            Regard's
            Thamayanthi.
            How many lines does your output contain approximately? The code worked for me. If this doesn't work, post your most recent code(whole script).

            Code:
            @r=$telnet->cmd('who');
            my $i=0;
            shift @r until($r[$i++]=~/^\[/); ## delete lines upto prompt
            
            pop @r if($r[$#r]=~/^\[.*\]$/);
            print @r;
            Regards,
            Nithin

            Comment

            Working...