VMware {code} Community
JV0613
Contributor
Contributor

PHP with Perl SDK won't run properly

Hi,

I have a simple Perl script (see below) that grabs the hosts from my vCenter and prints it out to the terminal.

#!/usr/bin/perl -w  
use strict;   
use VMware::VIRuntime;  

Opts::parse();  
Opts::validate();  
Util::connect();  

my $host_views = Vim::find_entity_views( view_type => 'HostSystem');
foreach my $host_view (@$host_views) {               
                print $host_view."=>";             
                print $host_view->name."\n";             
            }
   
Util::disconnect();

When I use PHP's shell_exec(), exec(), passthru(), or popen() to grab the output of the script, I keep getting this error from Apache:

PHP line:

$hosts = shell_exec("/var/www/cgi-bin/gethosts.pl --server <MyvCenterIP> --username <MyUsername> --password '<MyPassword>'");

Error after running PHP:

Error connecting to server at 'https://<MyvCenterIP>/sdk/webService': Perhaps host is not a vCenter or ESX server

But when I run the script in a terminal, it works fine.

# /var/www/cgi-bin/gethosts.pl --server <MyvCenterIP> --username <MyUsername> --password <MyPassword>
HostSystem=HASH(0x3340110)=> <Host1IP>
HostSystem=HASH(0x643b088)=> <Host2IP>
HostSystem=HASH(0x6707778)=> <Host3IP>

I also tried writing a Hello world script just to see if mod_perl is working fine with PHP and it was ok.

Just for more info, I am on CentOS 6. This script worked somewhat in Ubuntu with PHP but had other issues.

Any help would be greatly appreciated.

Reply
0 Kudos
3 Replies
natxoasenjo
Enthusiast
Enthusiast

try

$hosts = exec("/path/to/your/perl /var/www/cgi-bin/gethosts.pl --server  --username  --password ''");

not shell_exec

Reply
0 Kudos
JV0613
Contributor
Contributor

Thank you very much for the fast reply! What fixed it for me was adding the path to the perl binary as you suggested. 😄
Reply
0 Kudos
natxoasenjo
Enthusiast
Enthusiast

glad you got it working 😉

Reply
0 Kudos