Třída TSSH2 využívá funkce "Secure Shell2" implementované v PHP 5.x.x. Tyto funkce jsou založené na knihovně 'libssh2' která musí být v daném systému nainstalována. Ani s modulem SSH2 do PHP to není tak jednoduché, ten se musí stáhnout zvlášť a do PHP 'přidělat' a pak teprve je možné s ním celé PHP zkompilovat. Příslušné verze PHP, libssh2 a ssh2, které spolu pasují, jsou ke stažení zde.
<?php
require_once "tssh2.class.php";
try{
$ssh2 = new TSsh2("linux133.fzu.cz");
$ssh2->Login_Auth_Password("uzivatelske_jmeno","heslo");
$a_vystup = $ssh2->Send_Commands("/sbin/arp -i eth0 | grep ":" | grep -v "no match"");
print_r($a_vystup);
}
catch (exception $e){
echo $e;
}
?>
<?php
class TSsh2{
protected $connection;
public function __construct($hostname,$port=22){
if (!$this->connection = ssh2_connect($hostname,$port)) throw new exception("Unable connection. Incorrect hostname or port?");
}
public function Get_Auth_Method($username){
return ssh2_auth_none($this->connection,$username);
}
public function Login_Auth_Password($username,$password){
if (!ssh2_auth_password($this->connection,$username,$password)) throw new exception("Unable login via password. Incorrect username,password or this method is not allowed?");
}
public function Send_Commands($commands){
$a_output = array();
$a_command = explode("n",$commands);
$stdio = ssh2_shell($this->connection,"");
if (!$stdio) throw new exception("Unable execute interactive shell.");
foreach ($a_command as $command){
$command = trim($command);
if (($command{0}!="#")and($command{0}!=";")and($command{0}!="!")){
fwrite($stdio,"$commandn");
sleep(1);
flush();
while($a_output[] = fgets($stdio));
}
}
fclose($stdio);
return $a_output;
}
public function Send_Commands_File($filename){
if (!$commands = file_get_contents($filename)) throw new exception("Filename:'$filename' not exists.");
return $this->Send_Commands($commands);
}
public function __destruct(){
}
}
?>