We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

arunhem
arunhem asked
on
Medium Priority
234 Views
Last Modified: 2008-02-26
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.
Comment
Watch Question

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
oops: to run the remote cgi-script:

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

should do it afaik :-)

Author

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?
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.