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 '##############################';
}
?>