Sponsored Content
Full Discussion: perl ssh2 output formatting
Top Forums Shell Programming and Scripting perl ssh2 output formatting Post 302473548 by mtomar on Sunday 21st of November 2010 01:50:05 PM
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 ??
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy