PHP:How to connect to a unix box, run a script and ftp back the generated file to local box

I want to be able to connect to a unix box from my windows machine where I am running my PHP script. I would like to run my perl script on the unix box which generates a text file. I would then like to ftp file back to my local box for display on the web site. How can this be done in php.
arunhemAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAelbrechtCommented:
the perl script has to be web-accessible if you want it to be run by a remote host.
one of the things you can do is put it in your cgi-bin directory.
the text file it generates should be dumped (ofcourse) in an ftp-readable directory or (this is easier, imho) it could put it somewhere in one of your web-accessible directories.

depending on the option you choose (i'm assuming you want ftp):
ftp:
<?php
  $ftpconn = ftp_connect($ftpserver); //$ftpserver = IP or FQDN
  $ftplogin = ftp_login($ftpconn, $ftpuser, $ftppass);
  $fp =  fopen("path_to_where_file_has_to_be_saved", "w");
  if ($ftpconn && $ftplogin) { //both are booleans
     if (ftp_fget($ftpconn, $savefilepath, "remote_filename", FTP_ASCII)){
       echo "File successfully downloaded";
     }
  }
  ftp_close($ftpconn);
?>

(if you saved the file on a webaccessible server, it'd be something like this:)
<?php
  $remotefp = fopen("http://servername/filename.txt, "r");
  $fp = fopen(("path_to_where_file_has_to_be_saved", "w");
  do {
    $data = fread($remotefp, 4096);
    if strlen($data) == 0) {
      break;
    }
    fputs($fp, $data);
  }
  fclose($fp);
  fclose($remotefp);
?>

hope this helps,

cheers,
Andy
0
AndyAelbrechtCommented:
oops: to run the remote cgi-script:

<?php
  $runscript = fopen("http://servername/cgi-bin/script.pl");
  fclose($runscript);
?>

should do it afaik :-)
0
arunhemAuthor Commented:
So I would run first run my perl script and then do the ftp. So there is just no way to do without accessing this perl script through URL. I mean doesn't PHP provide anything that would allow you to run something like SSH and then run the script at the  remote server and then run the ftp to get the file back?
0
AndyAelbrechtCommented:
execute script thru ssh:

(from the php manual)

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection, '/usr/local/bin/php -i');

fclose($stream);
fclose($connection);
?>

hope this helps,

Andy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.