Ik ben pas geleden begonnen met OOP, ik denk dat ik het een beetje door heb nu. Nu zit ik wel nog met een aantal vragen. (Lees de comments in de code voor mijn vragen).
<?php
error_reporting(E_ALL);
class cSSHConnect
{
private $Host;
private $UserName;
private $PassWord;
private $Port;
private $Connection;
private $SFTP;
public $HomeFolder;
public $ScreenName;
public $MEMUsage;
public $CPULoad;
function __construct($Host, $UserName, $PassWord, $Port){
$this->Host = $Host;
$this->UserName = $UserName;
$this->PassWord = $PassWord;
$this->Port = $Port;
/* Bovenstaande is niet nodig toch? Beter is dit lijkt me:
*
* $this->Connection = ssh2_connect($Host, $Port);
* if (!@ssh2_auth_password($this->Connection, $UserName, $PassWord)) {
* trigger_error("Authentication failed");
* }
*
*/
# Zelfde geld voor onderstaande
$this->Connection = ssh2_connect($this->Host, $this->Port);
if (!@ssh2_auth_password($this->Connection, $this->UserName, $this->PassWord)) {
trigger_error("Authentication failed");
}
$this->SFTP = ssh2_sftp($this->Connection);
if (!$this->SFTP) {
trigger_error("Error bij het initializeren van SFTP");
}
}
private function GetPid($this->HomeFolder) {
# $this->FileName is hier ook onzinnig lijkt me? $FileName zou ook al voldoende zijn?
# Netzoals Config + PID + Result + SearchString
$this->FileName = "ssh2.sftp://".$this->SFTP.$HomeFolder."/pt_start-script";
$this->Config = file($this->FileName);
foreach ($this->Config as $Line) {
$this->SearchString = next($this->Config);
$this->Result = $this->PutCommand("ps -A -f | grep '".$this->SearchString."'");
preg_match("~^[\w\d]+\s+(\d+)~", $this->Result, $this->PID);
return $this->PID[1];
}
}
public function GameserverLoad($this->HomeFolder) {
$this->PID = $this->GetPid($this->HomeFolder);
$this->Result = $this->PutCommand("top -b -p ".$this->PID);
$this->SplitTop = explode("S", $this->Result);
$this->SplitTop = trim($this->SplitTop[6]);
$this->Load = explode(" ", $this->SplitTop);
$this->CPULoad = $this->Load[2];
$this->MEMUsage = $this->Load[0];
}
}
# Mag ik nu dit doen ->
$ssh = new SSHConnect("xx.xx.xx.xx", "user", "pw", "22");
$ssh->HomeFolder = "/home/user/";
$ssh->ScreenName = "pub_24" // wordt samengesteld door gegevens uit de DB
# Dit even puur als voorbeeld:
$ssh->GameserverLoad();
# Of moet het echt zo:
# De twee members weglaten bovenaan ($this->HomeFolder en $this->ScreenName);
# En de code binnen mijn class zo: public function GameserverLoad($HomeFolder) {
$ssh->GameserverLoad("/home/user");
?>
Alvast bedankt voor het meedenken :)
582 views