Net-SSH2 - net-ssh2 channel utilisation -2

Posted on Wed Jan 23 17:48:14 2008 by cpla
net-ssh2 channel utilisation -2
Here is a well formated of my preceding request: Hi, I try to use Net::SSH2 to build a specific process (to get alarm) for parsing on the "sys_549" server the result of a "vmstat" command that is executed on the "test_server". My problem is that : when I execute "vmstat 60 99999" with a "channel shell" , I receive only the first 3 lines like with the "vmstat" command. When I execute "vmstat 60 99999" with a "channel exec" , I have to wait the end of the "vmstat 60 99999" command to receive the all the lines at the same time. (in fact I don't wait. It's to long) My wish is to receive each line of data from the "vmstat 60 99999" one after each over like in a manual ssh session every 60 seconds (second part of the log file). Could you explain me where is my mistake ? You will find hereunder the perl script and the log file that illustrate my purpose. #!/usr/bin/perl # use Net::SSH2; use warnings; use strict; use Data::Dumper; use Text::ParseWords; my $server="test_server"; my $user="test_ruser1"; my $password="pwd_ruser1"; my $code=""; my $error_name=""; my $error_string=""; my $sortie=""; my $chan1=""; my $ssh2=Net::SSH2->new(); my $debug=0; $ssh2->debug(0); # connect print "Connection to $server\n"; $ssh2->connect($server) or die "Unable to connect Host $@ \n"; ( $code, $error_name, $error_string ) = $ssh2->error(); if ($code ne 0) { print "code = $code\n"; print "error_name = $error_name\n"; print "error_string = $error_string\n"; } else { if ($debug > 0) {print "Connection OK\n";} } # authenticate print "Authentification with the user $user\n"; $ssh2->auth_password($user,$password) or die "Unable to login $@ \n"; ( $code, $error_name, $error_string ) = $ssh2->error(); if ($code ne 0) { print "code = $code\n"; print "error_name = $error_name\n"; print "error_string = $error_string\n"; } else { if ($debug > 0) {print "Authentification OK\n";} } # DEBlock SSH2 for channel open if ($debug > 0) {print "DeBlocking SSH2\n";} $ssh2->blocking(1); # open channel if ($debug > 0) {print "Open Chan1\n";} $chan1 = $ssh2->channel(); ( $code, $error_name, $error_string ) = $ssh2->error(); if ($code ne 0) { print "code = $code\n"; print "error_name = $error_name\n"; print "error_string = $error_string\n"; } else { if ($debug > 0) {print "open chan1 OK\n";} } # Block SSH2 for channel shell if ($debug > 0) {print "Blocking SSH2\n";} $ssh2->blocking(0); ( $code, $error_name, $error_string ) = $ssh2->error(); if ($code ne 0) { print "code = $code\n"; print "error_name = $error_name\n"; print "error_string = $error_string\n"; } else { if ($debug > 0) {print "Blocking OK\n";} } # start channel shell $chan1->shell(); print $chan1 "\n"; print "LINE1 : $_" while <$chan1>; print $chan1 "vmstat\n"; print "LINE2 : $_" while <$chan1>; print $chan1 "vmstat 60 99999\n"; print "LINE3 : $_" while <$chan1>; # close channel shell $chan1->close; exit; and the log of the session : Console log with the perl process : [sup_user@sys_549 test]$ ./test_vmstat_ssh2.pl Connection to test_server Authentification with the user test_ruser1 LINE1 : Hewlett-Packard Company LINE1 : 3000 Hanover Street LINE1 : Palo Alto, CA 94304 U.S.A. LINE1 : Rights for non-DOD U.S. Government Departments and Agencies are as set LINE1 : forth in FAR 52.227-19(c)(1,2). LINE2 : procs memory page faults cpu LINE2 : r b w avm free re at pi po fr de sr in sy cs us sy id LINE2 : 1 0 0 89323 3306686 121 19 0 0 0 0 0 1621 4377 530 0 1 99 LINE3 : procs memory page faults cpu LINE3 : r b w avm free re at pi po fr de sr in sy cs us sy id LINE3 : 1 0 0 89323 3306530 121 19 0 0 0 0 0 1621 4377 530 0 1 99 [sup_user@sys_549 test]$ ****************************************************************************** Console log when connected to the remote server : [sup_user@sys_549 test]$ ssh test_ruser1@test_server test_ruser1@test_server's password: Last login: Thu Dec 13 06:00:27 2007 from s00998d-adm.pro Hewlett-Packard Company 3000 Hanover Street Palo Alto, CA 94304 U.S.A. Rights for non-DOD U.S. Government Departments and Agencies are as set forth in FAR 52.227-19(c)(1,2). $ vmstat p rocs memory page faults cpu r b w avm free re at pi po fr de sr in sy cs us sy id 1 0 0 93670 3306409 121 19 0 0 0 0 0 1621 4377 530 0 1 99 $ vmstat 1 5 procs memory page faults cpu r b w avm free re at pi po fr de sr in sy cs us sy id 1 0 0 93670 3306409 121 19 0 0 0 0 0 1621 4377 530 0 1 99 1 0 0 93670 3306409 8 0 1 0 0 0 0 1614 1121 431 0 0 100 1 0 0 95526 3306358 6 0 1 0 0 0 0 1618 1026 432 0 0 100 1 0 0 95526 3306358 4 0 1 0 0 0 0 1620 964 430 0 0 100 1 0 0 95526 3306358 3 0 1 0 0 0 0 1618 912 428 0 0 100 $ exit logout Connection to test_server closed. [sup_user@sys_549 test]$ Thanks in advance for your help. Charles.
Write a response