我很久以前用http://de.php.net/manual/en/book.ssh2.php模块用 PHP 写了一个“Eggdrop Webinterface”。
您可以通过 SSH 将此模块连接到您的 linux 服务器并提交 shell 命令。在我的 Web 界面中,我编写了一个 Shellscript 来完成所有工作(添加/删除/启动/停止等)并通过 php ssh2 模块执行。
<?php
class ssh
{
private $sql;
private $con;
private static $instance;
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new ssh();
}
return self::$instance;
}
function __construct()
{
$this->sql = sql::getInstance();
}
/* Verbindung über SSH aufnehmen */
public function connect ($rootid)
{
global $cfg;
$q = $this->sql->query("SELECT ROOT_SSH_IP, ROOT_SSH_USER, ROOT_SSH_PASS, ROOT_SSH_PORT FROM ".prfx."rootserver WHERE ROOT_ID = '".$rootid."'");
$r = $this->sql->content($q);
$blowfish = new Blowfish($cfg["BLOWFISHKEY"]);
$pass = $blowfish->Decrypt($r["ROOT_SSH_PASS"]);
$this->ssh_connect($r["ROOT_SSH_IP"], $r["ROOT_SSH_PORT"], $pass, $r["ROOT_SSH_USER"]);
return true;
}
/* Disconnect */
public function my_ssh_disconnect($reason, $message, $language) {
printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
return true;
}
/* Eigentliche ssh_connect Funktion */
public function ssh_connect($host, $port, $pass, $user="root")
{
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => '3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));
$callbacks = array();
$this->con = ssh2_connect($host, $port, $methods, $callbacks);
if (!$this->con) die('Connection failed');
else {
if (!ssh2_auth_password($this->con, $user, trim($pass))) {
die("login failed.");
}
}
return true;
}
/* Befehle ausführen */
public function ssh_exec ($cmd, $stderr=true)
{
if ($stderr == true)
{
$stream = ssh2_exec($this->con, $cmd);
$err_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($err_stream, true);
$result_err = stream_get_contents($err_stream);
if (empty($result_err))
{
stream_set_blocking($stream, true);
$out = stream_get_contents($stream);
return $out;
}
else {
return $result_err;
}
}
else {
$stream = ssh2_exec($this->con, $cmd);
stream_set_blocking($stream, true);
$out = stream_get_contents($stream);
return $out;
}
}
/* Verbindung beenden */
public function quit()
{
$stream = ssh2_exec($this->con, 'exit');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
return true;
}
}
就我而言,我使用加密密码将 Serveraccounts 存储在 MySQL 表中。
希望小课堂有所帮助