Welcome Guest, Not a member yet? Register   Sign In
SSH2 Library
#1

[eluser]Alfredor[/eluser]
Hello Guys,
I'm having some troubles, I'm trying to make a ssh2 library so I can connect remotely to my servers trough my website Big Grin.
Does anyone is willing to help me out? D:
This is what I got, not sure if it will work (Yeh, I'm a complete noob at this library stuff)

class Ssh2 {
function conect(){
{
$ssh2 = ssh2_connect($server, $port))){
echo "Failure: Unable to establish connection\n";
} else {
if(!ssh2_auth_password($ssh2, "$muser", "$mpass")) {
echo "Failure: Unable to authenticate\n";
} else {
echo "Success: Managing Remote Machine At ".$server;
}
#2

[eluser]33cent[/eluser]
Good point to start is reading PHP Manual, you got some examples there.

Also, keep in mind that SSH2 isn't supported by default installation of XAMPP, so you must enable it separately.

PHP Manual said this:
Quote:Windows binaries may be found at » http://snaps.php.net/. To install, download php_ssh2.dll to the folder specified by your php.ini file's extension_dir directive. Enable it by adding extension=php_ssh2.dll to your php.ini and restarting your web server.

extension_dir=c:/php5/exts/
extension=php_ssh2.dll
Linux, BSD, and other *nix variants can be compiled using the following steps:

Download and install » OpenSSL. If you install OpenSSL via your distribution's packaging system be sure to install the development libraries as well. This will typically be a package named openssl-dev, openssl_devel, or some variation thereof.
Download and install » libssh2. Typically this means executing the following command from the libssh2 source tree. ./configure && make all install.
Run the pear installer for PECL/ssh2: pear install ssh2
Copy ssh2.so from the directory indicated by the build process to the location specified in your php.ini file under extension_dir.
Add extension=ssh2.so to your php.ini
Restart your web server to reload your php.ini settings.
#3

[eluser]Alfredor[/eluser]
I'm using ubuntu and I have already installed all the needed files :] its just that codeigniter is new in my dictionary xD
#4

[eluser]attos[/eluser]
Do you really need it to be a CI app? Why re-invent the well.

I've been using AjaxTerm (available at http://antony.lesuisse.org/software/ajaxterm/).

I tried other "web-terminals" using Java applets before and the issue I found was that you need to connect to port 22 on the server. This is sometimes not allowed by corporate firewalls and you are not able to connect remotely.

AjaxTerm has the advantage that you can use standard SSL/HTTPS and have secure communications.

It requires Python 2.3 or above, which I assume it's already installed in your Ubuntu box.

Good luck
#5

[eluser]Alfredor[/eluser]
In fact Making things from scratch gives you another point of view about the software function itself, also I'm trying to make a platform using this methods and I understand that is easier to find a good php developer rather than a good javascript one. Anyways I'm doing it I just need some guidance about how to make a library for it since php only has 27 functions for this extension I don't think it would be the biggest deal ever or am I wrong?
#6

[eluser]attos[/eluser]
Let me try to guess what you're trying to achieve:

* You have a web server accessible from where you want to connect.
* Behind this web servers there are others that you want to connect.
* The web server will be used to accept commands, send them to the other servers, get their response and send it back to the browser.
* In other words, your web server will be used as a terminal.

If the above is true, then there are some issues that you need to deal with:
* The HTTP is a stateless protocol. This means that for each connection there is a request and a response.
* On anther hand, with SSH once a connection is established there can be more than one request and more than one response. Also requests and responses as asynchronous.

Now, if you only need to use a limited number of commands this can be achieved with less effort following something like:

connect to the server;
send a command;
get the response;
disconnect from the server;
send the response back to the browser.

Can you where I'm going? The user interface would be:
* a login page to your whole application
* a page with a way to select the sever and a way to accept the username/password. This page will also have a way to execute commands, either a text box or buttons with fixed commands. This page would also have an area to display the response from the previous command.

One thing that you need to take care of is the security. You do not want anybody to access your servers without your authorization and wreak things.

Sorry if I went overboard with this, but I had to make some assumptions in order to clarify things and be able to help.

Finally, I found an article that can help you as well: http://www.linuxformat.co.uk/wiki/index...._the_shell

Cheers!
#7

[eluser]Alfredor[/eluser]
Hey,
Almost there but its a little bit more complicated :].
I got a server structure, Front-End, Back-End And Gateway.
The front-end would be the login and stuff, the back-end will be the administration of the information(all servers and stuff are stored in a database) and the Gateway is a special VPS server to deal with the connections and the command sending stuff.
I was thinking on passing over the commands using XML-SOAP to send commands to the gateway server.
#8

[eluser]Alfredor[/eluser]
Hey Guys,
I have done some work already but there is something that still troubling me here is the code:

<?php

class Ssh2
{


function __construct()
{
$this->ci =& get_instance();
}

function conect($server,$port,$muser,$mpass)
{
if(!($con = ssh2_connect($server,$port))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con,$muser,$mpass)) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
}
}
}

function run_command($server,$port,$command)
{
$con = ssh2_connect($server,$port);
if(!($stream = ssh2_exec($con,$command)Wink ){
echo "fail: unable to execute command\n";
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = "";
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}

}


The error I get is:

A PHP Error was encountered

Severity: Warning

Message: ssh2_exec() [function.ssh2-exec]: Unable to request a channel from remote host

Filename: libraries/Ssh2.php

Line Number: 30
#9

[eluser]Alfredor[/eluser]
I've Found a Library but I Don't Know how to use it :/ Also its not documented, any Idea?
Posting Code:
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

/**
* SSH class using ssh2 extention
* for connecting and executing commands for linux server
*
* @author Shuky
*
*/
class CI_SSH {

var $hostname = 'hostname';
var $username = 'username';
var $password = 'password';
var $port = 22;
var $debug = FALSE;
var $conn_id = FALSE;
var $data = '';


/**
* Constructor - Sets Preferences
*
* The constructor can be passed an array of config values
*/
function CI_SSH() {
log_message('debug', "SSH Class Initialized");
}

/**
* Initialize preferences
*
* @access public
* @param array
* @return void
*/
function initialize($config = array()) {
foreach ($config as $key => $val) {
if (isset($this->$key)) {
$this->$key = $val;
}
}

// Prep the hostname
$this->hostname = preg_replace('|.+?://|', '', $this->hostname);
}

/**
* SSH Connect
*
* @access public
* @param array the connection values
* @return bool
*/
function connect($config = array()) {
if (count($config) > 0) {
$this->initialize($config);
}

if (FALSE === ($this->conn_id = @ssh2_connect($this->hostname, $this->port))) {
if ($this->debug == TRUE) {
$this->_error('ssh_unable_to_connect');
}
return FALSE;
}

if ( ! $this->_login()) {
if ($this->debug == TRUE) {
$this->_error('ssh_unable_to_login');
}
return FALSE;
}

return TRUE;
}

/**
* SSH Login
*
* @access private
* @return bool
*/
function _login() {
return @ssh2_auth_password($this->conn_id, $this->username, $this->password);
}

/**
* Validates the connection ID
*
* @access private
* @return bool
*/
function _is_conn() {
if ( ! is_resource($this->conn_id)) {
if ($this->debug == TRUE) {
$this->_error('ssh_no_connection');
}
return FALSE;
}
return TRUE;
}

/**
* Execute a command
*
* @access public
* @return stream
*/
function execute($command = "") {
if($this->_is_conn()) {
$stream = @ssh2_exec($this->conn_id, $command);
return $this->_get_stream_data($stream);
}
else {
$this->_error('fail: unable to execute command\n');
return FALSE;
}
}

/**
* Get stream data
*
* @access privte
* @return bool
*/
function _get_stream_data($stream) {
stream_set_blocking( $stream, true );
while( $buf = fread($stream,4096) ) {
$this->data .= $buf.'~';
}
return TRUE;
}

/**
* rename file or directory
*/
function rename($old_name , $new_name) {
if($this->_is_conn()) {
return @ssh2_sftp_rename($this->conn_id, $old_name , $new_name);
}
else return FALSE;
}

/**
* copy file from remote
*/
function recive($remote_file , $local_file) {
if($this->_is_conn()) {
return ssh2_scp_recv($this->conn_id, $remote_file , $local_file);
}
else {
$this->_error('fail: unable to execute command\n');
return FALSE;
}
}


/**
* Display error message
*
* @access private
* @param string
* @return bool
*/
function _error($line) {
$CI =& get_instance();
$CI->lang->load('ftp');
show_error($CI->lang->line($line));
}
}

// END FTP Class

/* End of file SSH.php */
/* Location: ./system/aplication/libraries/SSH.php */
#10

[eluser]attos[/eluser]
Hey afredor,

I'm back from a weekend break.
A couple of things you have to be careful with:
* Do not allow root to connect with ssh. This is a security hazard and has been exploited. You can have a normal user and give some privileges to only some commands and not the whole system.
* The SSH username and password are hard-coded.

Try the following:
* Put the ssh from your previous post in your application/libraries directory. For the following I assume the file name is ssh.php.
* Modify your Ssh2 class as follows (just check the code: brackets, commas, etc):

Code:
class Ssh2
{
  function __construct()
  {
      $this->load->library('ssh');
  }

  function connect($server,$port,$muser,$mpass)
  {
    $params = array(
        'hostname'=>$server,
        'port'=>$port,
        'username'=>$muser,
        'password'=>$mpass);
  if(!($con = $this->ssh->connect($params))){
      echo “fail: unable to establish connection\n”;
      return FALSE;
  }
  return $con;
}
//else {
//  // try to authenticate with username root, password secretpassword
//  if(!ssh2_auth_password($con,$muser,$mpass)) {
//      echo “fail: unable to authenticate\n”;
//  } else {
//      // allright, we’re in!
//      echo “okay: logged in…\n”;
//      }
//      }
//  }
    
  function run_command($server,$port,$command)
  {
    if ($con = $this->connect($server,$port,'root','secretpassword')
    {
//    $con = ssh2_connect($server,$port);
//    if(!($stream = ssh2_exec($con,$command)) ){
//        echo “fail: unable to execute command\n”;
//      } else{
//        // collect returning data from command
//        stream_set_blocking( $stream, true );
//        $data = "";
//        while( $buf = fread($stream,4096) ){
//          $data .= $buf;
//        }
//        fclose($stream);

        $data = $this->ssh->execute($command);
        if ($data)
        {
          // do something with your $data
        } else
        {
          // Something went wrong
        }

      }
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB