0

I have a bunch of server out there and I endavoured to create a php script to ssh all my servers, collect the last log entry and insert all results into a database.

I am using phpseclib to connect to the server, and one by one it works fine, but because I have a lot of them I though why not use threads ?

Well i am not quite sure what I am missing here but here what I got so far. outside of thread everything works fine but as soon as I thread NET_SSH2->login everything falls appart and nothing works.

Here my code any clues on what I missing ?

Thanks

<?php 
set_time_limit(0);
include('Net/SSH2.php');

class poller extends Thread{

private $tid;
private $tip_array;
public $tresult;

public function __construct($tid,$tip_array)
{
    $this->tid = $tid;
    $this->tip_array = $tip_array;
}

public function run()
{
    $i=1;
    foreach ($this->tip_array as $ip){
        $temp_result[$i][0]=$ip;
        $ssh = new Net_SSH2($ip[0]);
        if (!$ssh->login('user', 'pass')) {
             $temp_result[$i][1]= $ssh->isConnected() ? 'bad username or password' : 'unable to establish connection';
        }           
        $temp_result[$i][2]=$ssh->exec(' grep "Device" syslog.messages | tail -1'); 
        $i++;
    }
    $this->tresult=$temp_result;
} 
}

$i=1;
$rows=array(array("1.1.1.1", "2.2.2.2"), array("3.3.3.3", "4.4.4.4"));

foreach ($rows as $row){
    $threads[$i] = new poller($i,$row);
    $threads[$i]->start();
    $i++;
}

foreach ($threads as $thread)
{
    $thread->join();
    echo '############Thread'.$thread->tid.'############';
    print_r($thread->tresult);
    echo '##############################';
}
?>
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • "_everything falls appart and nothing works_". How do you mean? Do you get errors or something? In lieu of knowing what you mean it seems like SSH log files might help. You can get them by doing `define('NET_SSH2_LOGGING', 2)` at the top and then `$ssh->getLog()` for each instance of Net_SSH2. I also wonder if it's possible that Thread's don't work with `fsockopen`, which is what phpseclib uses under the hoods to implement SSH. – neubert Jun 17 '18 at 14:49
  • Hi Neubert, Thanks for the reply. I think you are absolutly right phpseclib does use fsockopen so I think that would be a no go. I'll try to find an alternative. – Bob Morane Jun 18 '18 at 16:56

0 Answers0