1

I'm trying to open multiple connections (various devices) to run a command and get the output. The problem is that i have to run them "all at once"/parallel. If i wait for one result and then to run the other one it takes way too long and with a large number of devices that can go very bad.

I'm also using curl which I know that there is curl_multi and I was wondering if there was something similar with SSH for php.

I'm using Net_SSH2 for now.

2
  • I think you should lift this up to a more abstract level; take a look at the process control extension (PCNTL). Commented Jul 29, 2015 at 11:46
  • Unfortunately i need this to be able to run on a Windows machine also Commented Jul 29, 2015 at 12:33

2 Answers 2

1

You'll need to use two PHP libraries: https://robo.li/tasks/Remote/#ssh and https://github.com/cheprasov/php-parallel. Your class method might be something similar to the example below:

function runParallelSSH() {

   $parallel = new Parallel(new ApcuStorage());

   foreach ($credentials as $user => $host) {
      $connection = sprintf('%s@%s', $user, $host);
      $connections[] = $connection;
      $Parallel->run($connection, function() {  
         $gitTask = $this->taskGitStack()
             ->checkout('master')
             ->pull();          
      });
   }

   $results = $parallel->wait($connections);

}
Sign up to request clarification or add additional context in comments.

1 Comment

You bet! I currently use Parallel in Production code to crawl web sites easily launching 500 processes at 100K+ HTTP requests an hour
0

Without using thirdparties like curl_multi you have to use PHP multithreading, for this you need an extension pthreads.

Look in the docs for PHP threading

The most interesting feature is using it like this (code modified from PHP.net)

class My extends Thread {
    public function run() {
        //curl_exec whatever
    }
}
$my = new My();

//start as many as you need
$my->start();

//wait for the threads to finnish and join one thread at a time with main-process-thread:
var_dump($my->join());:

Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.