0

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....
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1005246
  • 135
  • 3
  • 13
  • Show the SSH command that you're executing. – Barmar Sep 18 '15 at 20:22
  • 1
    Couple of things for you to edit into your question: What is the command being sent over SSH? By "kicks off the php" do you mean it runs the script? Or just PHP in general? If the script itself is running, try a `print_r($argv)` so you can see how PHP is seeing the arguments being passed. It may be that they're all lumped together in the first argument, or missing entirely, depending on what command is being sent over SSH. – Sam Graham Sep 18 '15 at 20:25
  • I updated the post to show log from SSIS SSH addon – user1005246 Sep 18 '15 at 20:39
  • 1
    This might not be it, but when you login non-interactively, your personal (~) and global (/etc) profile and bashrc scripts are not necessarily run, for me if I use ssh to run a single command on my arch server, /etc/profile, /etc/bash.bashrc, ~/.bash_profile or ~/.bashrc are all never run, leaving some environment options not set. You might want to try putting echo statements in all of these files and see which are being run and have a look at them in general as they vary quite a bit from system to system. – stellarpower Sep 19 '15 at 16:18
  • "When it runs it kicks off the php fine but ignores the arguments" <-- how have you diagnosed that, exactly? Also, first thing I'd try is to log in interactively, and use `which php` to check which PHP interpreter you're using. Then put the exact path to that in your ssh command rather than just `php`... – Matt Gibson Sep 21 '15 at 08:13

1 Answers1

0

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"];
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I tried the-n option..which should have used the default settings allowing register_argc_argv to be active and it did not work.. I AM DONE messing with this no need.. in a PHP script.. how can I override the args that are passed in and provide my own manually in the script? – user1005246 Sep 22 '15 at 13:13
  • The `register_argc_argv` is off by default. – Martin Prikryl Sep 22 '15 at 13:28
  • Anyway, what about copying `$_SERVER["argv"]` to `$argv`. See my edited answer. – Martin Prikryl Sep 22 '15 at 13:28
  • the issue was similar.. it had to do with the php version itself.. they use a custom jailed environment which does not provide direct access to /usr/bin/php so I had to call it directly at /usr/local/php54/bin/php – user1005246 Sep 22 '15 at 15:38