- fixed eol-style
This commit is contained in:
@@ -1,267 +1,267 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ssh Transport class
|
* ssh Transport class
|
||||||
*
|
*
|
||||||
* This class provides interaction with modules
|
* This class provides interaction with modules
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
* This file is part of the Froxlor project.
|
* This file is part of the Froxlor project.
|
||||||
* Copyright (c) 2010 the Froxlor Team (see authors).
|
* Copyright (c) 2010 the Froxlor Team (see authors).
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the COPYING
|
* For the full copyright and license information, please view the COPYING
|
||||||
* file that was distributed with this source code. You can also view the
|
* file that was distributed with this source code. You can also view the
|
||||||
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||||
*
|
*
|
||||||
* @category FroxlorCore
|
* @category FroxlorCore
|
||||||
* @package Classes
|
* @package Classes
|
||||||
* @subpackage System
|
* @subpackage System
|
||||||
* @author Froxlor Team <team@froxlor.org>
|
* @author Froxlor Team <team@froxlor.org>
|
||||||
* @copyright 2010 the authors
|
* @copyright 2010 the authors
|
||||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||||
* @version SVN: $Id: class.FroxlorModule.php 1167 2010-06-22 11:46:34Z d00p $
|
* @version SVN: $Id: class.FroxlorModule.php 1167 2010-06-22 11:46:34Z d00p $
|
||||||
* @link http://www.froxlor.org/
|
* @link http://www.froxlor.org/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FroxlorSshTransport
|
* Class FroxlorSshTransport
|
||||||
*
|
*
|
||||||
* This class handles remote server related stuff.
|
* This class handles remote server related stuff.
|
||||||
*
|
*
|
||||||
* @category FroxlorCore
|
* @category FroxlorCore
|
||||||
* @package Classes
|
* @package Classes
|
||||||
* @subpackage System
|
* @subpackage System
|
||||||
* @author Froxlor Team <team@froxlor.org>
|
* @author Froxlor Team <team@froxlor.org>
|
||||||
* @copyright 2010 the authors
|
* @copyright 2010 the authors
|
||||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||||
* @link http://www.froxlor.org/
|
* @link http://www.froxlor.org/
|
||||||
*/
|
*/
|
||||||
class FroxlorSshTransport
|
class FroxlorSshTransport
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Clas object.
|
* Clas object.
|
||||||
*
|
*
|
||||||
* @var FroxlorSshTransport
|
* @var FroxlorSshTransport
|
||||||
*/
|
*/
|
||||||
private static $_instance = null;
|
private static $_instance = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the connection handle.
|
* Contains the connection handle.
|
||||||
*
|
*
|
||||||
* @var resource
|
* @var resource
|
||||||
*/
|
*/
|
||||||
private $_connection = null;
|
private $_connection = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains all information for the ssh connection.
|
* Contains all information for the ssh connection.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $_settings = array();
|
private $_settings = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the shell resource.
|
* Contains the shell resource.
|
||||||
*
|
*
|
||||||
* @var resource
|
* @var resource
|
||||||
*/
|
*/
|
||||||
private $_shell = null;
|
private $_shell = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor (Singleton)
|
* Constructor (Singleton)
|
||||||
*
|
*
|
||||||
* @param array $settings contains an array of settings for the connection
|
* @param array $settings contains an array of settings for the connection
|
||||||
*
|
*
|
||||||
* @return FroxlorSshTransport
|
* @return FroxlorSshTransport
|
||||||
*
|
*
|
||||||
* @throws Exception if the connection could not be established
|
* @throws Exception if the connection could not be established
|
||||||
*/
|
*/
|
||||||
private function __construct($settings)
|
private function __construct($settings)
|
||||||
{
|
{
|
||||||
// settings
|
// settings
|
||||||
$this->_settings = $settings;
|
$this->_settings = $settings;
|
||||||
// try to connect
|
// try to connect
|
||||||
$this->_connect();
|
$this->_connect();
|
||||||
|
|
||||||
// get a shell stream
|
// get a shell stream
|
||||||
$this->_shell = $this->_getShell();
|
$this->_shell = $this->_getShell();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for publickey auth
|
* Constructor for publickey auth
|
||||||
*
|
*
|
||||||
* @param string $ip ip toremote server
|
* @param string $ip ip toremote server
|
||||||
* @param string $port remote port
|
* @param string $port remote port
|
||||||
* @param string $username ssh username
|
* @param string $username ssh username
|
||||||
* @param string $pubkeyfile path to pubkeyfile
|
* @param string $pubkeyfile path to pubkeyfile
|
||||||
* @param string $privkeyfile path to privatekeyfile
|
* @param string $privkeyfile path to privatekeyfile
|
||||||
* @param string $passphrase passphrase
|
* @param string $passphrase passphrase
|
||||||
*
|
*
|
||||||
* @return FroxlorSshTransport
|
* @return FroxlorSshTransport
|
||||||
*/
|
*/
|
||||||
public static function usePublicKey($ip, $port, $username, $pubkeyfile, $privkeyfile , $passphrase)
|
public static function usePublicKey($ip, $port, $username, $pubkeyfile, $privkeyfile , $passphrase)
|
||||||
{
|
{
|
||||||
$settings = array(
|
$settings = array(
|
||||||
'ip' => $ip,
|
'ip' => $ip,
|
||||||
'port' => $port,
|
'port' => $port,
|
||||||
'username' => $username,
|
'username' => $username,
|
||||||
'pubkeyfile' => $pubkeyfile,
|
'pubkeyfile' => $pubkeyfile,
|
||||||
'privkeyfile' => $privkeyfile,
|
'privkeyfile' => $privkeyfile,
|
||||||
'passphrase' => $passphrase
|
'passphrase' => $passphrase
|
||||||
);
|
);
|
||||||
|
|
||||||
if (is_null(self::$_instance)) {
|
if (is_null(self::$_instance)) {
|
||||||
self::$_instance = new FroxlorSshTransport($settings);
|
self::$_instance = new FroxlorSshTransport($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$_instance;
|
return self::$_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param string $ip ip toremote server
|
* @param string $ip ip toremote server
|
||||||
* @param string $port remote port
|
* @param string $port remote port
|
||||||
* @param string $username ssh username
|
* @param string $username ssh username
|
||||||
* @param string $password ssh password
|
* @param string $password ssh password
|
||||||
*
|
*
|
||||||
* @return FroxlorSshTransport
|
* @return FroxlorSshTransport
|
||||||
*/
|
*/
|
||||||
public static function usePlainPassword($ip, $port, $username, $password)
|
public static function usePlainPassword($ip, $port, $username, $password)
|
||||||
{
|
{
|
||||||
$settings = array(
|
$settings = array(
|
||||||
'ip' => $ip,
|
'ip' => $ip,
|
||||||
'port' => $port,
|
'port' => $port,
|
||||||
'username' => $username,
|
'username' => $username,
|
||||||
'password' => $password
|
'password' => $password
|
||||||
);
|
);
|
||||||
|
|
||||||
if (is_null(self::$_instance)) {
|
if (is_null(self::$_instance)) {
|
||||||
self::$_instance = new FroxlorSshTransport($settings);
|
self::$_instance = new FroxlorSshTransport($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$_instance;
|
return self::$_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a command to the shell session.
|
* Send a command to the shell session.
|
||||||
*
|
*
|
||||||
* @param string $cmd command to send (without EOL)
|
* @param string $cmd command to send (without EOL)
|
||||||
*/
|
*/
|
||||||
public function sendCmd($cmd)
|
public function sendCmd($cmd)
|
||||||
{
|
{
|
||||||
// writes the command to the shell
|
// writes the command to the shell
|
||||||
fwrite($this->_shell, $cmd.PHP_EOL);
|
fwrite($this->_shell, $cmd.PHP_EOL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a file to the remote server.
|
* Sends a file to the remote server.
|
||||||
*
|
*
|
||||||
* @param string $localFile path to the local file
|
* @param string $localFile path to the local file
|
||||||
* @param string $remoteFile remote file path
|
* @param string $remoteFile remote file path
|
||||||
* @param string $chmod file rights (default: 0644)
|
* @param string $chmod file rights (default: 0644)
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function sendFile($localFile, $remoteFile, $chmod = 0644)
|
public function sendFile($localFile, $remoteFile, $chmod = 0644)
|
||||||
{
|
{
|
||||||
// check if file exists
|
// check if file exists
|
||||||
if (@!is_readable($localFile)) {
|
if (@!is_readable($localFile)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// send file
|
// send file
|
||||||
if (ssh2_scp_send($this->_connection, $localFile, $remoteFile, $chmod)) {
|
if (ssh2_scp_send($this->_connection, $localFile, $remoteFile, $chmod)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This reads all available output.
|
* This reads all available output.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function readAll()
|
public function readAll()
|
||||||
{
|
{
|
||||||
if (is_null($this->_shell)) {
|
if (is_null($this->_shell)) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = array();
|
$output = array();
|
||||||
|
|
||||||
while($line = fgets($this->_shell)) {
|
while($line = fgets($this->_shell)) {
|
||||||
$output[] = $line;
|
$output[] = $line;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean up.
|
* Clean up.
|
||||||
*/
|
*/
|
||||||
public function close()
|
public function close()
|
||||||
{
|
{
|
||||||
$this->_shell = null;
|
$this->_shell = null;
|
||||||
$this->_settings = null;
|
$this->_settings = null;
|
||||||
$this->_connection = null;
|
$this->_connection = null;
|
||||||
$this->_instance = null;
|
$this->_instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function connects to the remote server.
|
* This function connects to the remote server.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function _connect()
|
private function _connect()
|
||||||
{
|
{
|
||||||
$callbacks = array('disconnect' => array('FroxlorSshTransport', 'disconnected'));
|
$callbacks = array('disconnect' => array('FroxlorSshTransport', 'disconnected'));
|
||||||
|
|
||||||
if ($this->_settings['pubkeyfile']) {
|
if ($this->_settings['pubkeyfile']) {
|
||||||
// pubkey authentication
|
// pubkey authentication
|
||||||
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array('hostkey'=>'ssh-rsa'), $callbacks);
|
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array('hostkey'=>'ssh-rsa'), $callbacks);
|
||||||
$success = ssh2_auth_pubkey_file($this->_connection, $this->_settings['username'], $pubkeyfile, $privkeyfile, $passphrase);
|
$success = ssh2_auth_pubkey_file($this->_connection, $this->_settings['username'], $pubkeyfile, $privkeyfile, $passphrase);
|
||||||
} else {
|
} else {
|
||||||
// plain password authentication
|
// plain password authentication
|
||||||
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array(), $callbacks);
|
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array(), $callbacks);
|
||||||
$success = ssh2_auth_password($this->_connection, $this->_settings['username'], $this->_settings['password']);
|
$success = ssh2_auth_password($this->_connection, $this->_settings['username'], $this->_settings['password']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check connection
|
// check connection
|
||||||
if (!$success) {
|
if (!$success) {
|
||||||
// TODO change this to an Exception for froxlor 1.0
|
// TODO change this to an Exception for froxlor 1.0
|
||||||
throw new Exception("Connection to ssh could not be established!");
|
throw new Exception("Connection to ssh could not be established!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a shell.
|
* Returns a shell.
|
||||||
*
|
*
|
||||||
* @return resource
|
* @return resource
|
||||||
*/
|
*/
|
||||||
private function _getShell()
|
private function _getShell()
|
||||||
{
|
{
|
||||||
return ssh2_shell($this->_connection, 'vt102', null, 80, 24, SSH2_TERM_UNIT_CHARS);
|
return ssh2_shell($this->_connection, 'vt102', null, 80, 24, SSH2_TERM_UNIT_CHARS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback function if the connection disconnects.
|
* Callback function if the connection disconnects.
|
||||||
*
|
*
|
||||||
* @param string $reason reason
|
* @param string $reason reason
|
||||||
* @param string $message message
|
* @param string $message message
|
||||||
* @param string $language language
|
* @param string $language language
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function disconnected($reason, $message, $language)
|
public static function disconnected($reason, $message, $language)
|
||||||
{
|
{
|
||||||
// try reconnecting
|
// try reconnecting
|
||||||
try{
|
try{
|
||||||
self::$_instance->_connect();
|
self::$_instance->_connect();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
die("Connection lost and could not re-established! \n".$reason."\n".$message."\n".$language."\n".$e->getMessage());
|
die("Connection lost and could not re-established! \n".$reason."\n".$message."\n".$language."\n".$e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -1,46 +1,46 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* This file is for demonstration purpose and should NEVER be enabled
|
* This file is for demonstration purpose and should NEVER be enabled
|
||||||
* in a live enviroment.
|
* in a live enviroment.
|
||||||
*
|
*
|
||||||
* @version SVN: $Id: class.FroxlorModule.php 1167 2010-06-22 11:46:34Z d00p $
|
* @version SVN: $Id: class.FroxlorModule.php 1167 2010-06-22 11:46:34Z d00p $
|
||||||
*/
|
*/
|
||||||
exit();
|
exit();
|
||||||
|
|
||||||
/* get a FroxlorSshTransport Object with plain password authentication
|
/* get a FroxlorSshTransport Object with plain password authentication
|
||||||
* Note: to get this working you have to enable plain password authentication in
|
* Note: to get this working you have to enable plain password authentication in
|
||||||
* your sshd config!
|
* your sshd config!
|
||||||
*/
|
*/
|
||||||
$transport = FroxlorSshTransport::usePlainPassword("test.froxlor.org", 22, "testSshUser", "plainpassword");
|
$transport = FroxlorSshTransport::usePlainPassword("test.froxlor.org", 22, "testSshUser", "plainpassword");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Send a command to the ssh shell.
|
* Send a command to the ssh shell.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$transport->sendCmd("ls -alF");
|
$transport->sendCmd("ls -alF");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The return result is an array with lines read from stdin
|
* The return result is an array with lines read from stdin
|
||||||
*/
|
*/
|
||||||
$outputArray = $transport->readAll();
|
$outputArray = $transport->readAll();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Let's copy our sshd_config to the remote host.
|
* Let's copy our sshd_config to the remote host.
|
||||||
*/
|
*/
|
||||||
$transport->sendFile("/etc/ssh/sshd_config", "/etc/ssh/sshd_config", 0644);
|
$transport->sendFile("/etc/ssh/sshd_config", "/etc/ssh/sshd_config", 0644);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Close this session.
|
* Close this session.
|
||||||
*/
|
*/
|
||||||
$transport->close();
|
$transport->close();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create a new ssh session with pubkey authentication.
|
* Create a new ssh session with pubkey authentication.
|
||||||
*/
|
*/
|
||||||
$transport = FroxlorSshTransport::usePublicKey("test.froxlor.org", 22, "testUserSSh", "/path/to/pubkey.key", "/path/to/private.key", "myPassphrase1337");
|
$transport = FroxlorSshTransport::usePublicKey("test.froxlor.org", 22, "testUserSSh", "/path/to/pubkey.key", "/path/to/private.key", "myPassphrase1337");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Clean up and finish.
|
* Clean up and finish.
|
||||||
*/
|
*/
|
||||||
$transport->close();
|
$transport->close();
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user