perl ssh2 output formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl ssh2 output formatting
# 1  
Old 11-21-2010
perl ssh2 output formatting

Hi Experts,

I am using following format for Perl SSH2 commands.

HTML Code:
#!/usr/bin/perl -w
use Net::SSH::Perl;
use POSIX;
use Sys::Hostname;
use Term::ANSIColor qw(:constants);
use strict;

#my $host="10.128.0.214";
my @nodeip = ("10.128.0.214","10.128.0.215") ;
my %node = ("10.128.0.214","NODE-HOST1","10.128.0.215","NODE-HOST2") ;
my $hlckdate = strftime "%Y-%m-%d",localtime ;
my $user="user" ;
my $pass="passwd";
my $cmd ="uname -a";
my $host = hostname;

foreach (@nodeip) {
    my $ssh = Net::SSH::Perl->new($_,protocol =>'2',port =>22 );
    $ssh->login($user, $pass);
    $ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; print $buffer->bytes;});
    print "\n\n==========================================================================================\n";
    print "            PERFORMING  CHECK FOR $node{$_} \@ $hlckdate    \n";
    print "==========================================================================================\n"; 
   
    print BOLD,"OS INFO: ", RESET;
    $ssh->cmd($cmd);
    print BOLD,"\nSYS UPTIME: ",RESET;
    $ssh->cmd('uptime');
     print BOLD,"\nSYS RUN LEVEL:",RESET;
    $ssh->cmd('who -r');
     print BOLD,"\nFILESYSTEM INFO:\n",RESET;
    $ssh->cmd('df -h');
    
Output is as expected:
HTML Code:
==========================================================================================
            PERFORMING  CHECK FOR NODE-HOST2 @ 2010-11-21    
==========================================================================================
OS INFO: Linux host2 2.4.21-40.ELsmp #1 SMP Thu Feb 2 22:22:39 EST 2006 i686 i686 i386 GNU/Linux

SYS UPTIME:  16:08:01  up 219 days, 19:51,  1 user,  load average: 0.06, 0.06, 0.02

SYS RUN LEVEL:         run-level 3  Dec  4 16:52                   last=S  

FILESYSTEM INFO:
Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p2     985M  233M  702M  25% /
/dev/cciss/c0d0p1      97M   26M   67M  28% /boot
/dev/vg00/home        4.9G  1.5G  3.2G  32% /home
/dev/vg00/log         2.9G  1.7G  1.2G  60% /log
/dev/vg00/opt         5.8G  950M  4.6G  17% /opt
none                  2.5G     0  2.5G   0% /dev/shm
/dev/vg00/tmp         2.9G   41M  2.7G   2% /tmp
/dev/vg00/usr         5.8G  3.0G  2.6G  54% /usr
/dev/vg00/var         2.0G  418M  1.5G  23% /var
I have been trying to collect output of command in an array to process content.
If i can do something like
HTML Code:
my @tmpout = "output from previous command ( say df -h )"
foreach (@tmpout){
..
}
I have been searching a lot but no success till now.

Any help and hints?
Thanks in advance.

---------- Post updated at 01:50 PM ---------- Previous update was at 08:19 AM ----------

I have made this small change
HTML Code:
 $ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; my $output = $buffer->bytes;});
Expecting normal usage, I made following changes:
HTML Code:
print BOLD,"OS INFO: ", RESET;
    $ssh->cmd($cmd);
print $output ; 

#OR
my @out2 = print $output;
I am getting following error for each line where i am using $output.

HTML Code:
Global symbol "$output" requires explicit package name at ./scriptname.pl line XX
Any Clues ??
# 2  
Old 11-23-2010
As you are using $output in your example code, it is a lexically scoped declaration which is only visible from the point of declaration to the end of the innermost enclosing block.

Try the following to see the difference:
Code:
....
my $output="";

foreach (@nodeip) {
    my $ssh = Net::SSH::Perl->new($_,protocol =>'2',port =>22 );
    $ssh->login($user, $pass);
    $ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; $output = $buffer->bytes;});

    print BOLD,"OS INFO: ", RESET;
    $ssh->cmd($cmd);
    print $output;
}

# 3  
Old 11-23-2010
Thanks a lot for suggestion. I actually already tried same.I did not update case. Since we define $output as empty, Even if i assign it to an array or try to print it ...output is still empty.

HTML Code:
my $tmpout="";
$ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; my $tmpout = print $buffer->bytes;});

#Calling  $tmpout 
$ssh->cmd('ifconfig -a | grep 192.168.35.216');
    my @chksys = $tmpout;
    print @chksys ;
    print @chksys ;
Now if this trick would work i should get 3 lines of cmd output. But i get just one line. i.e. only from $ssh->cmd. since $tmpout is "null" .

I have been reading about using Net::SSH::Perl::Buffer and
$ssh->incoming_data to force output into array. Still nothing.

It seems nothing is ringing bell for me as of now...
Requirement is pretty simple but it appear solution is not that simple

Smilie
# 4  
Old 11-23-2010
Change
Code:
$ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; my $tmpout = print $buffer->bytes;});

to
Code:
$ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; $tmpout = $buffer->bytes;});

# 5  
Old 11-24-2010
I tried that too, Still nothing.
if i dont define
Code:
my $tmpout

In global code.
it get same error. if i define it.
It prints 1 for each print command corresponding to $tmpout.
In my experience removing / applying my or even variable name with print command in "sub" of register handler has no effect at all.

There must be some option if not basic than in advance.
I even tried defining like
Code:
my $outdata = $ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; print $buffer->bytes;});
..
print $outdata;
    my @chksys = $outdata;
    print @chksys ;

I get HASH values as out put of print comands:
Code:
HASH(0x10699e80)

However
Code:
$ssh->cmd($cmd);

Prints normal output without any issues.
# 6  
Old 11-24-2010
You are doing something wrong. It works perfectly fine for me.

Please provide copy of your updated script with the changes I suggested - nothing else -
and I will see what is wrong.
# 7  
Old 11-24-2010
Code:
#!/usr/bin/perl -w
use Net::SSH::Perl;
#use Net::SSH::Perl::Buffer (my @args);
use POSIX;
use Sys::Hostname;
use Term::ANSIColor qw(:constants);
use strict;

my $tmpout="";
#my $host="192.168.35.214";
my @nodeip = ("192.168.35.214","192.168.35.215") ;
my %node = ("192.168.35.214","NODE-HOST1","192.168.35.215","NODE-HOST2") ;
my $hlckdate = strftime "%Y-%m-%d",localtime ;
my $user="user" ;
my $pass="passwd";
my $cmd ="uname -a";
my $host = hostname;
#my $buffer = Net::SSH::Perl::Buffer->new;

foreach (@nodeip) {
    my $ssh = Net::SSH::Perl->new($_,protocol =>'2',port =>22,#debug => 1
    );
    $ssh->login($user, $pass);
    $ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; $tmpout = print $buffer->bytes;});
    #$ssh->register_handler("stdout",sub {my ($channel,$buffer) = @_; print $buffer->get_str;});
    
    print "\n\n==========================================================================================\n";
    print "            PERFORMING CHECK FOR $node{$_} \@ $hlckdate    \n";
    print "==========================================================================================\n"; 
   
    print BOLD,"OS INFO: ", RESET;
    $ssh->cmd($cmd);
    print BOLD,"\nSYS UPTIME: ",RESET;
    $ssh->cmd('uptime');
     print BOLD,"\nSYS RUN LEVEL:",RESET;
    $ssh->cmd('who -r');
     print BOLD,"\nFILESYSTEM INFO:\n",RESET;
    $ssh->cmd('df -h');
    print "\n\n CHECKING IF CURRENT SERVER IS ACTIVE ..............\n\n";
    $ssh->cmd('ifconfig -a | grep 192.168.35.216');
    print $tmpout;
    my @chksys = $tmpout;
    print @chksys ;
    print @chksys ;
        foreach (@chksys){
                             if(/192.168.35.216/){
                         print "----------------------------------------------------------------\n";
                         print "$host is active server.....performing other checks....\n";
                         print "----------------------------------------------------------------\n\n";
                         $ssh->cmd('ps -ef | grep manager');
                         #my nodeinfo1 = print @tmpout ;
                         print "----------------------------------\n";
                         print "NODE SERVICE:";
                         print "----------------------------------\n\n";
                         #foreach(@nodeinfo1){
                             #if(/grep/){    
                             #}else{
                             #    print $_;
                             #}
                        }
                      }
                    }

OUTPUT
Code:
==========================================================================================
            PERFORMING CHECK FOR NODE-HOST1 @ 2010-11-24    
==========================================================================================
OS INFO: Linux host1 2.4.21-40.ELsmp #1 SMP Thu Feb 2 22:22:39 EST 2006 i686 i686 i386 GNU/Linux

SYS UPTIME:  20:46:33  up 13 days,  8:14,  0 users,  load average: 0.00, 0.01, 0.00

SYS RUN LEVEL:         run-level 3  Nov 11 12:32                   last=S  

FILESYSTEM INFO:
Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p2     985M  273M  663M  30% /
/dev/cciss/c0d0p1      97M   26M   67M  28% /boot
/dev/vg00/home        4.9G  3.2G  1.5G  69% /home
/dev/vg00/informix    985M  469M  466M  51% /informix
/dev/vg00/log         2.9G  779M  2.0G  28% /log
/dev/vg00/opt         5.8G  993M  4.6G  18% /opt
none                  2.5G     0  2.5G   0% /dev/shm
/dev/vg00/tmp         2.9G   41M  2.7G   2% /tmp
/dev/vg00/usr         5.8G  3.0G  2.5G  55% /usr
/dev/vg00/var         2.0G  625M  1.3G  34% /var


 CHECKING IF CURRENT SERVER IS ACTIVE ..............

111

==========================================================================================
            PERFORMING CHECK FOR NODE-HOST2 @ 2010-11-24    
==========================================================================================
OS INFO: Linux host2 2.4.21-40.ELsmp #1 SMP Thu Feb 2 22:22:39 EST 2006 i686 i686 i386 GNU/Linux

SYS UPTIME:  20:46:53  up 223 days, 30 min,  0 users,  load average: 0.05, 0.08, 0.03

SYS RUN LEVEL:         run-level 3  Dec  4 16:52                   last=S  

FILESYSTEM INFO:
Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p2     985M  233M  702M  25% /
/dev/cciss/c0d0p1      97M   26M   67M  28% /boot
/dev/vg00/home        4.9G  1.5G  3.2G  32% /home
/dev/vg00/informix    985M  323M  612M  35% /informix
/dev/vg00/log         2.9G  1.9G  896M  69% /log
/dev/vg00/opt         5.8G  950M  4.6G  17% /opt
none                  2.5G     0  2.5G   0% /dev/shm
/dev/vg00/tmp         2.9G   41M  2.7G   2% /tmp
/dev/vg00/usr         5.8G  3.0G  2.6G  54% /usr
/dev/vg00/var         2.0G  434M  1.5G  24% /var
/dev/vgraid/cdr       2.0G   33M  1.9G   2% /cdr
/dev/vgraid/prompts   2.0G   97M  1.8G   6% /prompts
/dev/vgraid/archive    20G   13G  6.5G  66% /archive
/dev/vgraid/ready     2.0G   34M  1.9G   2% /ready
/dev/vgraid/data      9.9G  2.0G  7.5G  21% /data
/dev/vgraid/mcx       9.9G  3.5G  6.0G  37% /mcx


 CHECKING IF CURRENT SERVER IS ACTIVE ..............

          inet addr:192.168.35.216  Bcast:192.168.35.255  Mask:255.255.255.0
111

See 111 at the end of output. It is because of
Code:
print $tmpout;
    my @chksys = $tmpout;
    print @chksys ;
    print @chksys ;

While i am expecting
Code:
inet addr:192.168.35.216  Bcast:192.168.35.255  Mask:255.255.255.0

print is just for check. As you can see in code above i want to run some commands if string have 192.168.35.216 in it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting of output

Hi, I have some output in the format below: Col-A Col-B 8781 4319 8781 2332 8781 0269 5550 3282 5550 9465 5550 7607 7064 4456 . . I want to re-format the output so i will get something like this: 8781:4319,2332,0269 5550:3282,9465,7607 7064:4456 for... (6 Replies)
Discussion started by: james2009
6 Replies

2. Shell Programming and Scripting

Formatting output

I have the output like below: DEV#: 9 DEVICE NAME: hdisk9 TYPE: 1750500 ALGORITHM: Load Balance SERIAL: 68173531021 ========================================================================== Path# Adapter/Path Name State Mode Select Errors 0 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

3. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

4. Shell Programming and Scripting

Perl ssh2 login issue.

Hi Experts, I came across this interesting situation. I have following ssh script login to multiple server. This works fine for one set of servers (linux) but on my sun boxes i am getting. error #!/usr/bin/perl -w use Net::SSH::Perl; use POSIX; use Term::ANSIColor qw(:constants); use... (2 Replies)
Discussion started by: mtomar
2 Replies

5. Shell Programming and Scripting

Formatting Perl Output

Hi, I am getting the output from a PERL program but the output is all on different lines. My code is printf hOUT "%s\t%s\t%s\t%s\t%s\t%s",$M1_CTR, $M2_CTR, $M3_CTR, $M4_CTR, $M5_CTR, $M6_CTR; Example: My report (which I send via email) should look like this Area 1 Area 2 ... (4 Replies)
Discussion started by: nurani
4 Replies

6. Shell Programming and Scripting

Formatting my output

Dear All, I am new to unix scripting. I need your help to format my output on screen. echo " --------------------------------------------" echo " | My Output |" echo " --------------------------------------------" echo " | A: $A... (5 Replies)
Discussion started by: rahiljavaid
5 Replies

7. Shell Programming and Scripting

Formatting ls output

I am using find and ls to search for "warez" files on my server. find /home/ -regex ".*\.\(avi\|mp3\|mpeg\|mpg\|iso\)" -print0 | xargs -0 ls -oh This command produces this: -rw-r--r-- 1 1000 3.2M Feb 18 2009 /home/user/public_html/lupus.mp3 I want to only get this 3.2M... (4 Replies)
Discussion started by: bonrad
4 Replies

8. Shell Programming and Scripting

formatting the output

Is it possible to convert the attached file to the format mentioned. Here for a particular job the table name and the corresponding instance name from one test run "X" is compared with the table name and the instance name from the second test run "Y" for output rows,affected rows,applied... (1 Reply)
Discussion started by: ragavhere
1 Replies

9. Shell Programming and Scripting

Formatting Output

Hi I tried running the below awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}' from the command prompt and it is not working. Getting the error awk: Field $() is not correct. The source line number is 1. Actually my requirement is to form a string based on... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

10. Shell Programming and Scripting

formatting output

Hi need some advice.. #grep -i hostname test.csv (gives the below output) HOSTNAME,name,host_test,,,,,,,, Now I need to format the above output as below. HOSTNAME: name=host_test Any easy way of doing this using awk or sed or printf? (4 Replies)
Discussion started by: balaji_prk
4 Replies
Login or Register to Ask a Question