The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl - How to print a "carriage return" to an output file? teiji Shell Programming and Scripting 5 03-06-2009 03:44 AM
capturing output from top and format output new2ss Shell Programming and Scripting 4 02-24-2009 09:26 PM
How to print log file in column format using awk suresh3566 Shell Programming and Scripting 2 05-06-2008 02:18 PM
Check for proper e mail id format prolay Shell Programming and Scripting 2 08-23-2007 05:37 AM
How to print current month - 1 in string format mogli4 Shell Programming and Scripting 3 03-10-2006 04:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2009
gsprasanna gsprasanna is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 16
Net::SSH::Perl ...... how to print the output in a proper format

Hi Guys,

my $cmd = "ls -l"; #........ {or let it be as # my $cmd= "ls"; }
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd("$cmd");
print $stdout;

the script works fine, but i am unable to see the output getting displayed in a correct format.
i.e output of the above script is as follows:

total 79936 -rw-r--r-- 1 readonly bin 306 Feb 13 2008 a1.sh -rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog1.sh -rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog2.sh -rw-r--r-- 1 readonly bin 1248 Feb 13 2008 ca.log -rw-r--r-- 1 readonly bin 38143 Jul 17 2008 17072008_Run2 drwxr-xr-x 2 readonly bin 4096 Apr 9 2008 s -rw-r--r-- 1 readonly bin 14739 Jul 17 2008 07172008_Run2 -rw-r--r-- 1 readonly bin 15152 Jul 16 2008 17_07_2008


Instead of displaying the output as,

total 79936
-rw-r--r-- 1 readonly bin 306 Feb 13 2008 a1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog2.sh
-rw-r--r-- 1 readonly bin 1248 Feb 13 2008 ca.log
drwxr-xr-x 2 readonly bin 4096 Apr 9 2008 s
-rw-r--r-- 1 readonly bin 14739 Jul 17 2008 Analysis_07172008_Run2
-rw-r--r-- 1 readonly bin 15152 Jul 16 2008 17_07_2008


Please anyone let me know your valuable suggestions to display the o/p as
total 79936
-rw-r--r-- 1 readonly bin 306 Feb 13 2008 a1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog2.sh
-rw-r--r-- 1 readonly bin 1248 Feb 13 2008 ca.log
drwxr-xr-x 2 readonly bin 4096 Apr 9 2008 s
-rw-r--r-- 1 readonly bin 14739 Jul 17 2008 Analysis_07172008_Run2
-rw-r--r-- 1 readonly bin 15152 Jul 16 2008 17_07_2008


Not only for 'ls' command, but executing any commands using Net::SSH::Perl connection displays the output as a single line.

Please let me know your valuable suggestions.

Thanks in Advance
  #2 (permalink)  
Old 04-27-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972
In the future, please use [code ][/code ] tags, it improves readability.

What's the output if you change the last line to print ref($stdout)? Maybe it doesn't return a string but an array of strings.
  #3 (permalink)  
Old 04-27-2009
gsprasanna gsprasanna is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 16
As i tried replacing last line with print ref($stdout); displays a blank output (i.e no output)
  #4 (permalink)  
Old 04-27-2009
gsprasanna gsprasanna is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 16
Also if i replace last two lines of code as ,
my(@stdout, $stderr, $exit) = $ssh->cmd("$cmd");
print @stdout;

i see no difference between $stdout & @stdout outputs.
  #5 (permalink)  
Old 04-27-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972
If ref() returns an empty string that means that it isn't a reference to anything. And assigning a scalar to an array just makes that scalar the first element of the array.

I guess that the newline gets lost somewhere during transfer (either dropped by the server or Net::SSH::Perl itself) since in the code they're only appending one line of returned data to the previous lines, returning all of it in $stdout/$stderr.
  #6 (permalink)  
Old 04-27-2009
gsprasanna gsprasanna is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 16
Yes, you are right which is expected.

But i am looking for some way/solution where we can print the output as if executing a command in a unix box displays the output in a clear format. At present i am using CGIPerl Script to display the output on a browser and i am able to print the output on a browser in this way ....

total 79936 -rw-r--r-- 1 readonly bin 306 Feb 13 2008 a1.sh -rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog1.sh -rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog2.sh -rw-r--r-- 1 readonly bin 1248 Feb 13 2008 ca.log -rw-r--r-- 1

Last edited by gsprasanna; 04-27-2009 at 10:25 AM..
  #7 (permalink)  
Old 04-27-2009
gsprasanna gsprasanna is offline
Registered User
  
 

Join Date: Jul 2007
Location: India
Posts: 16
Though at present i am having a solution where we can split the output of 'ls' command as the output will be of this format ...

-rw-rw-rw- 1 root dir 104 Dec 25 19:32 filename

we can split the above output as fields i.e at filename field and we can assign this to an array and there by we can print the output.
i.e
total 79936
-rw-r--r-- 1 readonly bin 306 Feb 13 2008 a1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog1.sh
-rw-r--r-- 1 readonly bin 216 Oct 11 2007 accesslog2.sh
-rw-r--r-- 1 readonly bin 1248 Feb 13 2008 ca.log
drwxr-xr-x 2 readonly bin 4096 Apr 9 2008 s


But this is just a temporary solution which works fine for 'ls -l' command. But tomorrow if we wish to execute other commands such as "ls", "cat" and so on... it wont work.

But i appreciate 4ur thoughts & ur precious time.....
Still lets hope for a solution where we can print the output in a format wrapped at every newline of output and display the same on a browser.

Thanks in Advance,
GS
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0