2 Replies Latest reply on Aug 12, 2011 1:20 PM by Bill Robinson

    How do you execute blcli or blcli_execute commands from a perl script?


      I have a perl script that parses a csv file, removes unwanted charaters etc and finishes by constructing an NSH script.   I would prefer to run the blcli_execute commands within this perl script but i have been unable to get that working


      `blcli etc etc` doesn't seem to work. 


      does anyone have an example of this?  i am running this as a type 3 script BTW






      #!/opt/local/bin/perl -w

      # vi:set wm=0 ai sm:



      use strict;



      # get the filename from the command line

      my $file = $ARGV[1];



      # open the file and suck it into memory

      open(IN, $file) or die "Can't open $file: $!\n";

      my @in = <IN>;




      # now turn the array into one big scalar variable

      my $huge = join("", @in);

      # and re-split it on newlines so it makes sense in the loop

      @in = split(/\n/, $huge);



      my $cmd_file = "/$file.nsh";

      if (!open(CMD, ">$cmd_file"))


                die "Can't open the command file: $!\n";




      print CMD "#!/bin/nsh\n\n";

      print CMD "# Set up the persistent Java VM\n";

      print CMD "blcli_setoption authType BLSSO && blcli_setoption serviceProfileName Prod && blcli_setoption roleName BLAdmins && blcli_connect\n";


      # remove the first line from the file as the headers line

      my $headers = shift(@in);

      my @attrs = split(/,/, $headers);

      # lose the first column




      # go through and remove any surrounding quotes

      foreach my $attr (@attrs)


                $attr =~ s/^['"]+//;

                $attr =~ s/['"]+$//;




      # iterate through the file, one line at a time

      foreach my $line (@in)


                # skip empty lines

                next if ($line eq "");

                # remove newline characters at the end (i know, it doesn't make

                # much sense)


                $line =~ s/^/"/;

                $line =~ s/$/"/;

                $line =~ s/,/","/g;

                # split the line into pieces by using commas as the delimiter

                my @bits = split(/,/, $line);

                $bits[0] = lc($bits[0]);



                # note that you can take the first column as $bits[0]

                # and you can get the rest of them as @bits[1..$#bits]



                # iterate through the pieces we just split up

                foreach my $bit (@bits)


                          # this is where the new character will end up

                          my $newbit = "";

                          # make an array that's full of single characters

                          # (splitting on // means that each charaacter is split

                          # out)

                          my @chars = split(//, $bit);



                          # iterate through the characters

                          foreach my $char (@chars)


                                    # ord() returns the decimal value of a

                                    # character, see man ascii for values.  for

                                    # instance, A is 65.  this if() expression says

                                    # "take anything bigger than or equal to

                                    # 32 (space) and smaller than or equal to 126

                                    # (tilde)"

                                    if (ord($char) >= 32 && ord($char) <= 126)


                                              # ...and save it (.= means append)

                                              $newbit .= $char;


                                    # there's no else here, but if there were, it

                                    # would be saying what to do with characters

                                    # that *aren't* between 32 and 126


                          # remove leading...

                          $newbit =~ s/^['"]+//;

                          # ...and trailing quote characters (just in case)

                          $newbit =~ s/['"]+$//;

                          # save it without quotes

                          $bit = $newbit;


                # now that all the bits have been resaved without garbage chars,

                # and with quotes, re-join them with commas (the split() above

                # actually removes the commas entirely as a side-effect), then

                # print them out to STDOUT

                #print join(",", @bits), "\n";



                # remove the first column from the line and save it as $server

                my $server = shift(@bits);

                $server = lc($server);

                # print the "create server" command to the commands file

                print CMD "\n# Adding $server to Blade Logic\n";

                print CMD "blcli_execute Server addServer $server true\n";

                print CMD "blcli_storeenv RESULT\n";

                print CMD "echo $server: \$RESULT\n\n";

                print CMD "# Adding attributes for $server\n";



                # and for the rest of the columns...

                for (my $i=0; $i<=$#bits; $i++)


                          # skip columns with no header

                          next if ($attrs[$i] eq "");

                          # skip columns with no data

                          next if ($bits[$i] eq "");

                          # print the command out to our commands file

                          print CMD "blcli_execute Server setPropertyValueByName $server \"$attrs[$i]\" \"$bits[$i]\"\n";

                          print CMD "blcli_storeenv RESULT\n";

                          print CMD "echo $server, $bits[$i]: \$RESULT\n";





      close(CMD) or warn "Couldn't close $cmd_file: $!\n";



      print "Saved commands to $cmd_file\n";