Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

AIX harddisk sum of storage

by pjzero@90 (Novice)
on Jul 22, 2018 at 17:21 UTC ( [id://1219048]=perlquestion: print w/replies, xml ) Need Help??

pjzero@90 has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl monks, I'm trying to go through 250 aix lpars and capture. I SSH into each host and attempting to run a for loop within backticks, come to find out this does not work, it fails when it gets to "do" Any ideas or workarouns? Thanks.

sub aixstorage { my $all_servers = 'path to my AIX list of servers'; my @hosts=`cat $all_servers`; my $filename = '/tmp/aixstorage_report.rtf'; foreach my $one (@hosts){ chomp $one; my $ping = `ping -c 1 -w 1 $one 1>/dev/null 2>&1`; if (defined ($ping)) { my $SSH ="/usr/bin/ssh -o ConnectTimeout=3 -o Stri +ctHostKeyChecking=no"; my $space = $SSH root\@$one for i in lspv | awk '{pri +nt \$1}'; do bootinfo -s \$i; done | awk '{sum+=\$1}END{print (sum\/1 +024)}'}'"`; print "$space\n"; }else { print "\nATTENTION: $one is offline\n" } } }

Replies are listed 'Best First'.
Re: AIX harddisk sum of storage
by Corion (Patriarch) on Jul 22, 2018 at 17:40 UTC

    Your script contains at least one typo in the ssh invocation. Where exactly are you having problems?

    my $space = $SSH root\@$one for i in lspv | awk '{print \$1}'; do boo +tinfo -s \$i; done | awk '{sum+=\$1}END{print (sum\/1024)}'}'"`; # ^ here a backquote is missing print "$space\n";

    My suggestion would be to first print out the command locally to see whether it looks like you intend to, or whether a dollar sign or backslash gets interpolated away.

    I would move the summation to Perl instead of keeping it in awk, but that's more a stylistic issue.

      this is what I see when I run the script, does not like the "do"

      sh: -c: line 0: syntax error near unexpected token `do' sh: -c: line 0: `SSH root@server19 for i in lspv | awk '{print $1}';do + bootinfo -s $i;done' 256 sh: -c: line 0: syntax error near unexpected token `do' sh: -c: line 0: `SSH root@server20 for i in lspv | awk '{print $1}';do + bootinfo -s $i;done' 256 sh: -c: line 0: syntax error near unexpected token `do' sh: -c: line 0: `SSH root@server21 for i in lspv | awk '{print $1}';do + bootinfo -s $i;done' 256 sh: -c: line 0: syntax error near unexpected token `do' sh: -c: line 0: `SSH root@server22 for i in lspv | awk '{print $1}';do + bootinfo -s $i;done'

      Yet when I run it on an AIX server, things work just fine

      root@server01:/] # for i in `/usr/sbin/lspv | awk '{print $1}'`; do bo +otinfo -s $i; done | awk '{sum+=$1}END{print (sum/1024)}' 589.73

        Your working command includes backticks while your Perl string does not contain them.

        If you want to send backticks to the remote side, you will need to include them in the string, and hope that you get all the shell quoting correct. Personally, I prefer to use $() shell-syntax instead of backticks as that makes the quoting much easier. Instead of backticks in Perl, I will use qx(). That gives me

        my $command = qq[$SSH root\@$one for i in \$(lspv | awk '{print \$1}') +; do bootinfo -s \$i; done | awk '{sum+=\$1}END{print (sum\/1024)}']; warn "[[$command]]"; my $space = qx($command)

        I don't know if I've translated your shell code properly, as to me the original seems to contain a lone double quote and some spurious single quotes.

Re: AIX harddisk sum of storage
by haukex (Archbishop) on Jul 22, 2018 at 21:05 UTC

    I've had good experiences with Net::OpenSSH, so you might want to take a look at Net::OpenSSH::Parallel. As the others have commented, get a basic version working first, then expand it to run the more complex command you're showing.

Re: AIX harddisk sum of storage
by Lotus1 (Vicar) on Jul 22, 2018 at 20:20 UTC
    my $space  = $SSH root\@$one for i in lspv | awk '{print \$1}';

    It looks to me like everything past $SSH is going to be seen as barewords on this line. There is a lot being attempted so maybe the best approach is to simplify. Comment out two thirds of this code and get it working before adding in each line one at a time. For us to help you please show a complete program, using warnings and strict, and provide the warnings and errors you are seeing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1219048]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found