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

Hi,

First time user hopefully long time Monk. I have a question regarding Net::SSH::Perl which I hope can be easily answered.

Using Net::SSH::Perl I can get STDOUT to my screen in all cases except for commands run in real time.


For example tail -f or snoop.


I have no problems with such commands with defined answers such as who,cat,date,uptime etc.


This issue has a work around in SSH::Telnet using the code below.


Using Net::Telnet I can get a real time response to a tail -f or a snoop command using the following code
$telnet = new Net::Telnet (input_log => \*STDOUT , Timeout =>1000); $telnet ->open("$host"); $telnet->login("$user","$password"); $telnet->cmd("snoop -d bge0");


My question is, is there a workaround in Net::SSH::Perl


My Net::SSH code is


my $ssh=Net::SSH::Perl->new("$host"); $ssh->login("$user","$password"); ($stdout,$stderr)=$ssh->cmd("snoop -d bge0"); print $stdout;


but for the life of me I can't get this to work on Net::SSH::Perl

Replies are listed 'Best First'.
Re: Net::SSH::Perl Realtime output
by anthski (Scribe) on Sep 02, 2005 at 02:59 UTC
    You can do it using the register_handler function that Net::SSH::Perl provides.

    For example

    #!/usr/bin/perl -w use Net::SSH::Perl; use strict; my $host = "localhost"; my $username = "anthski"; my $password = "somepassword"; my $ssh = Net::SSH::Perl->new($host, debug => 0, protocol => '2'); my $command = "tail -f /var/log/messages"; my $loginStatus = $ssh->login($username,$password); my $stdout; my $stderr; my $exit; $ssh->register_handler("stdout", sub { # This will match any packets which are stdout and print them +to the screen # You could do something smarter with them if you wanted my($channel, $buffer) = @_; my $str = $buffer->bytes; print "$str\n"; }); ($stdout,$stderr,$exit) = $ssh->cmd($command);

    As per my comment in the code, you can then do whatever you want with the output of your command.

    cheers,
    Anth

      Anth,

      Cheers,that saved me alot more heartache and is much appreciated. Needless to say it worked a treat.

Re: Net::SSH::Perl Realtime output
by idsfa (Vicar) on Sep 01, 2005 at 19:19 UTC

    Are you, perhaps, suffering from buffering?


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon

      Thanks for the replies guys. In reply to your help sh1tn first. I probably didn't explain myself very well. I could get that example with "uname -a" to work but I couldn't get it to work with an example that is Interactive. For example I could get it to work with
      "tail 10 /var/adm/messages"
      but couldn't get it to work with
      "tail -f /var/adm/messages"
      as this has the ability to be changing all the time where the former is just a snapshot.


      In answer to idsfa,you could possibily be correct but if I can get it working for Net::Telnet It should work for Net::SSH::Perl. Besides, I could understand that being the case for a snoop maybe but not a tail -f on a small file

      Searched a fair bit around the net looking for an answer but can't find one as yet. Any other suggestions would be greatly appreciated


      Shooter.
Re: Net::SSH::Perl Realtime output
by sh1tn (Priest) on Sep 01, 2005 at 16:16 UTC
    use Net::SSH::Perl; my $host = '10.0.0.1'; my $ssh = Net::SSH::Perl->new($host); my $username = 'test_user'; my $password = 'test_pass'; my $command = 'uname -a'; $ssh->login($username, $password); print+($ssh->cmd($command))[0], "\n"; __END__ output: Linux somehost 2.6.12 #4 Fri Jul 15 10:24:21 EEST 2005 i686 unknown un +known GNU/Linux
    Works fine for me if I understand the question.