views:

27

answers:

0

I need to launch an ssh shell to a remote server for remote port forwarding in the background from a PHP script using the exec() function.

The command when run in the background is $execStr, so I use exec($execStr ." > /dev/null &"); $execStr works fine manually, but not in the script.

What is the correct syntax?

Here is the rogue program.

#!/usr/bin/php
<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 $handle = fopen('accountno', 'r');
 if ($handle) {
   $accountNo = fgets($handle);
   $acc = substr($accountNo, 4, 4);
   $execStr = "ssh -R $acc"."1:localhost:22 -R $acc"."2:localhost:5432  -R $acc"."3:localhost:10000   -i ~/remoteAdmin.key [email protected]";

   print("$execStr\n");
   $pid = exec("$execStr > /dev/null &");

   $pidHandle = fopen("rempid","w");
   fwrite($pidHandle, $pid);
   fclose($pidHandle);
   echo "program got to the end\n";
 }
?>