phpsshssiscommand-line-argumentsnon-interactive

PHP script with arguments run over non-interactive SSH session not working


I have to call a PHP script that takes arguments..

php /home/flintarm/www/store/magmi-importer/cli/magmi.cli.php -profile=Category -mode=create -logger=CLILogger

This works perfectly fine if I login with PuTTY and have an interactive login.

I am trying to call this command line from within a SSIS add-on task. (COZYROC SSH Execute Task, doesn't really matter) which seems to work non-interactively.

When it runs it kicks off the php fine but ignores the arguments (so it is not fine)

Below is the PHP source..

<?php

/**
 * MAGENTO MASS IMPORTER CLI SCRIPT
 * 
 * version : 0.1
 * author : S.BRACQUEMONT aka dweeves
 * updated : 2010-08-02
 * 
 */

require_once(dirname(dirname(__FILE__))."/inc/magmi_defs.php");
require_once('magmi_loggers.php');
$script=array_shift($argv);

function buildOptions($argv)
{
    $options=array();
    foreach($argv as $option)
    {
        $isopt=$option[0]=="-";

        if($isopt)
        {
            $optarr=explode("=",substr($option,1),2);
            $optname=$optarr[0];
            if(count($optarr)>1)
            {
                $optval=$optarr[1];
            }
            else
            {
                $optval=1;
            }
            $options[$optname]=$optval;
        }
    }
    return $options;
}


function getClassInstance($cval,$cdir=".")
{
    $cdef=explode(":",$cval);
    $cname=$cdef[0];
    $cclass=$cdef[1];
    $cinst=null;
    $cfile="$cdir/$cname.php";
    if(file_exists($cfile))
    {
        require_once($cfile);
        if(class_exists($cclass))
        {
            $cinst=new $cclass();               
        }
    }
    if($cinst==null)
    {
     die("Invalid class definition : ".$cval);
    }
    return $cinst;

}

function getEngineInstance($options)
{
    if(!isset($options["engine"]))
    {
        $options["engine"]="magmi_productimportengine:Magmi_ProductImportEngine";
    }
    $enginst=getClassInstance($options["engine"],dirname(dirname(__FILE__))."/engines");
    return $enginst;
}

$options=buildOptions($argv);
$importer=getEngineInstance($options);
if(isset($importer))
{
    $loggerclass=isset($options['logger'])?$options['logger']:"FileLogger";
    $importer->setLogger(new $loggerclass());
    if(!isset($options["chain"]))
    {
        $options["chain"]=isset($options["profile"])?$options["profile"]:"";
        $options["chain"].=isset($options["mode"])?":".$options["mode"]:"";
    }
    $pdefs=explode(",",$options["chain"]);
    foreach($pdefs as $pdef)
    {
         $pm=explode(":",$pdef);
         $eargv=array();
         if(!empty($pm[0]))
         {
             $eargv[]="-profile=".$pm[0];
         }
         if(isset($pm[1]))
         {
            $eargv[]="-mode=".$pm[1];
         }
        $eoptions=buildOptions($eargv);
        $importer->run(array_merge($eoptions,$options));
    }
}
?>

I don't have the exact SSH command since it is within the code of the SSIS component. The "verbose" log it produces shows this:

2015-09-18 16:36:13.459 VERBOSE SshSession(1)[6] SSH: Received packet SSH_MSG_USERAUTH_SUCCESS (1 bytes).
 0000 |34                                             | 4
2015-09-18 16:36:13.459 DEBUG SshSession(1)[6] SSH: Authentication successful.
2015-09-18 16:36:13.459 VERBOSE SshSession(1)[6] SSH: Sending packet SSH_MSG_CHANNEL_OPEN (24 bytes).
 0000 |5A-00-00-00-07-73-65-73 73-69-6F-6E-00-00-00-00| Z....session....
 0010 |00-02-00-00-00-00-40-00                        | ......@.
2015-09-18 16:36:13.490 VERBOSE SshSession(1)[6] SSH: Received packet SSH_MSG_CHANNEL_OPEN_CONFIRMATION (17 bytes).
 0000 |5B-00-00-00-00-00-00-00 00-00-00-00-00-00-00-80| [...............
 0010 |00                                             | .
2015-09-18 16:36:13.490 DEBUG SshSession(1)[6] SSH: Executing command 'php /home/flintarm/www/store/magmi-importer/cli/magmi.cli.php -profile=Category -mode=create -logger=CLILogger'.
2015-09-18 16:36:13.490 VERBOSE SshSession(1)[6] SSH: Sending packet SSH_MSG_CHANNEL_REQUEST (128 bytes).
 0000 |62-00-00-00-00-00-00-00 04-65-78-65-63-01-00-00| b........exec...
 0010 |00-6E-70-68-70-20-2F-68 6F-6D-65-2F-66-6C-69-6E| .nphp /home/flin
 0020 |74-61-72-6D-2F-77-77-77 2F-73-74-6F-72-65-2F-6D| tarm/www/store/m
 0030 |61-67-6D-69-2D-69-6D-70 6F-72-74-65-72-2F-63-6C| agmi-importer/cl
 0040 |69-2F-6D-61-67-6D-69-2E 63-6C-69-2E-70-68-70-20| i/magmi.cli.php 
 0050 |2D-70-72-6F-66-69-6C-65 3D-43-61-74-65-67-6F-72| -profile=Categor
 0060 |79-20-2D-6D-6F-64-65-3D 63-72-65-61-74-65-20-2D| y -mode=create -
 0070 |6C-6F-67-67-65-72-3D-43 4C-49-4C-6F-67-67-65-72| logger=CLILogger
2015-09-18 16:36:13.537 VERBOSE SshSession(1)[6] SSH: Received packet SSH_MSG_CHANNEL_WINDOW_ADJUST (9 bytes).
 0000 |5D-00-00-00-00-00-20-00 00                     | ]..... ..
2015-09-18 16:36:13.537 VERBOSE SshSession(1)[6] SSH: Received packet SSH_MSG_CHANNEL_SUCCESS (5 bytes).
 0000 |63-00-00-00-00                                 | c....

Solution

  • When you start a non-interactive session, a different set of startup scripts are run and/or different paths in the scripts are taken (based on TTY variable).

    So your environment may (most probably does) differ.

    This might affect the PHP script execution. In your case, you may not have the register_argc_argv enabled. So the $argv is not set.

    I'd suggest you to rewrite your script not to rely on a non-default PHP settings. Particularly use the $_SERVER["argv"] instead of the $argv.

    Or put the phpinfo() at the beginning of your script, run it both interactively and non-interactively, and see what causes the difference in the PHP configuration. Then, correct your startup scripts to set the same PHP configuration for both interactive and non-interactive sessions.


    Or if you do not want to mess with the script, put this at the very beginning:

    $argv = $_SERVER["argv"];