DataSift\Storyplayer\CommandLib\SshClient::runCommand PHP Method

runCommand() public method

public runCommand ( string $command ) : CommandResult
$command string
return CommandResult
    public function runCommand($command)
    {
        // vet our input
        Contract::RequiresValue($command, is_string($command));
        Contract::RequiresValue($command, !empty($command));
        // make the params printable / executable
        // $printableParams = $this->convertParamsForUse($params);
        // what are we doing?
        $log = usingLog()->startAction("run command '{$command}' against host ");
        // do we actually have everything we need to run the command?
        if (!$this->hasSshUsername()) {
            throw new E4xx_NeedSshUsername();
        }
        if (!$this->hasIpAddress()) {
            throw new E4xx_NeedIpAddress();
        }
        // build the full command
        //
        // the options that we pass (by default) to SSH:
        //
        // -o StrictHostKeyChecking=no
        //    do not verify the SSH host key (avoids an interactive prompt)
        // -i <private_key>
        //    use specified private key to access the remote/guest OS
        // <username>@<hostname>
        //    who we are logging in as (should never be root)
        // <additional SSH options>
        //    any other flags, such as -n to force non-interactive session
        // <command> <command-args>
        //    the command to run on the remote/guest OS
        //    (assumes the command will be globbed by the remote shell)
        $fullCommand = 'ssh ' . ' ' . $this->getSshKeyForUse() . ' ' . $this->getSshOptionsForUse() . ' ' . $this->getSshUsername() . '@' . $this->getIpAddress() . ' "' . str_replace('"', '\\"', $command) . '"';
        // run the command
        //$log->startStep("run command via SSH: {$fullCommand}");
        $commandRunner = $this->st->getNewCommandRunner();
        $result = $commandRunner->runSilently($fullCommand);
        //$log->endStep();
        // all done
        $log->endAction("return code was '{$result->returnCode}'");
        return $result;
    }

Usage Example

Esempio n. 1
0
 /**
  * @covers DataSift\Storyplayer\CommandLib\SshClient::runCommand
  * @covers DataSift\Storyplayer\CommandLib\E4xx_NeedIpAddress::__construct
  */
 public function testMustProvideIpAddressToRunCommands()
 {
     // ----------------------------------------------------------------
     // setup your test
     // our mocked $st
     $st = Mockery::mock("DataSift\\Storyplayer\\PlayerLib\\StoryTeller");
     // our fake Output for logging purposes
     $output = Mockery::mock("DataSift\\Storyplayer\\Output");
     $output->shouldReceive('logPhaseActivity');
     // our fake injectables container
     $injectables = new Injectables();
     $injectables->dataFormatter = new DataFormatter();
     $injectables->dataFormatter->setIsVerbose(true);
     $injectables->output = $output;
     // our mocked $st needs to do things too
     //
     // we have to put this down here, because we need to pass $st into
     // our mocked CommandRunner
     $st->shouldReceive('startAction')->once()->andReturn(new Action_LogItem($injectables, 1));
     // our test subject
     $obj = new SshClient($st);
     $obj->setSshUsername('fred');
     // ----------------------------------------------------------------
     // perform the change
     $caughtException = false;
     try {
         $result = $obj->runCommand('ls /tmp');
     } catch (E4xx_NeedIpAddress $e) {
         $caughtException = true;
     }
     // ----------------------------------------------------------------
     // test the results
     $this->assertTrue($caughtException);
     // all done
     Mockery::close();
 }