Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

PHP page with perl

Hi guys I hope you can clear this up for me.

I have the following situation.


Client MachineA
Client MachineB
ServerA
ServerB

--------------------------------------
Clients A and B will open up a web page and point to http://serverA/index.php

On ServerA,
I have perl scripts that when I run locally (that is, logged into ServerA through telnet), target the server called ServerB. These perl scripts only exist on ServerA to connect to and retrieve information from Server B.

What Im hoping is possible is the following.....

Clients open up a web page to ServerA.
Coded in PHP on ServerA eg.index.php on ServerA, are perl commands that launch the perl scripts on ServerA, that point to ServerB, collect information back from ServerB, and display on the index.php page to the Clients.....that is, the Clients click on buttons on serverA/index.php, which retrieve information about ServerB, and displayed to these clients.

So, in other words.....

ClientA/B ------------> http://serverA/index.php ----------------------> results of connect.pl
                                          clientA user                                                 displayed in index.php
                                          clicks on a button                                           to users/s at clientA/B
                                          which launches
                                   perl connect.pl -s serverB

Is this possible, or would I have to publish the serverA/index.php as a citirx web application since the perl scripts being executed ONLY exist on serverA? Or, because php is server-side, if I code into the index.php page a perl script which exists only locally on serverA, can the users of clientA/B launch these perl scripts, which would execute FROM serverA?

Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of miqrogroove
miqrogroove
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On serverA/index.php you can use CURL to execute your perl script on server b:
//serverA/index.php
<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.ServerB.com/connect.php");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$response = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

echo $response;
?>

ServerB/connect.php
<?php
$output = shell_exec('perl connect.pl -s');
echo$output;
?>
For more info look at the curl examples here:
http://us3.php.net/manual/en/function.curl-exec.php
Avatar of Simon336697

ASKER

Hi hielo!

Thank you so much for your kind answers hielo.

hielo,

the perl scripts are physically located on SERVERA.

It is on ServerA that I wish to execute them.

The perl scripts simply reference ServerB in their commands.

So basically, I want users of clientA and B machines to open up a web page to serverA.

http://serverA/index.php

In the code of index.php, is some code which executes a perl script physically located on serverA.

The results of that execution of the perl script are then returned to the clientA's browser in serverA/index.php

Is the curl statement still the way to go in this case?
I think all you need is echo shell_exec()
>>Is the curl statement still the way to go in this case?
No. No needed at all. All you need in that case is to use this as your ServerA/index.php:
<?php
$output = shell_exec('perl connect.pl -s');
echo $output;
?>
Thank you again you nice people.
Im having problems with formatting in the shell_exec statement the following....

At a command line, it looks like the following...


E:\>perl datacenterlisting.pl --server serverB --datacenter "Out Data Center" --username domain\username --password "password"

When i kick off the above, it works.

How guys do I put this into:

<?php
$output = shell_exec('perl connect.pl -s');
echo $output;
?>

where the 'perl connect.pl -s'
is replaced with...

E:\>perl datacenterlisting.pl --server serverB --datacenter "Out Data Center" --username domain\username --password "password"

<?php
echo shell_exec('perl datacenterlisting.pl --server serverB --datacenter "Out Data Center" --username domain\username --password "password"');
?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi mi, hielo!

Guys serverA is a windows box.

Do I need to escape backslashes ?

eg.

Would
perl e:\datacenterlisting.pl
become
perl e:\\datacenterlisting.pl (double backslash?

echo shell_exec('perl e:\\datacenterlisting.pl --server serverB --datacenter "Out Data Center" --username domain\username --password "password"');
I believe so. Try it, but be consistent. You are not escaping it on domain\username. Try it like this:

echo shell_exec('perl e:\\datacenterlisting.pl --server serverB --datacenter "Out Data Center" --username domain\\username --password "password"');

If that does not work, try it with single slashes, but again, be consistent.
NO do not add slashes!  Slashes only get escaped in double-quoted strings.
You guys are fantastic.
That works like as follows...

<?php
echo shell_exec('perl e:\datacenterlisting.pl --server serverB --datacenter "name" --username domain\username --password "password"');
?>


I cant thank both you, hielo, and miqrogroove, enough.
All the very best to you both.