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

[eluser]Alfredor[/eluser]
Hello again Folks, I've been trying to get this thing working, but I still have some problems, the connect function works wonderfully, but the execute function is not for some reason.... here is the code again.. XD
Code:
<?php

class Ssh2
{


    function __construct()
    {
        $this->ci =& get_instance();
    }
    
    function connect($server,$port,$muser,$mpass)
    {
    if(!($con = ssh2_connect($server,$port))){
    echo "<font color='red'><b>Failure: Unable To Connect</b></font>";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con,$muser,$mpass)) {
        echo "<font color='red'><b>Failure: Unable To Authenticate</b></font>";
    } else {
        // allright, we're in!
        echo "<font color='green'><b>Success: Logged In, Connected At ".$server." With User ".$muser." And Password ".$mpass."</b></font>";
        }
        }
    }
        
   function execute($server,$port,$command)
   {
       $con = ssh2_connect($server,$port);
      if(!($stream = ssh2_exec($con,$command)) ){
            echo "<font color='red'><b>Failure: Unable To Execute Command</b></font>";
        } else{
            // collect returning data from command
            stream_set_blocking($stream, TRUE);
            $data = "";
            while( $buf = fread($stream, 4096) ){
                $data .= $buf;
            }
            fclose($stream);
        }
   }  
  
   function download($server,$port,$muser,$mpass,$rfile,$lfile)
   {
   $con = ssh2_connect($server,$port);
   ssh2_auth_password($con,$muser,$mpassword);
   ssh2_scp_recv($con,$rfile,$lfile);
   }
}

I actually want to colect the data that is returning thats why its begin so anoying.
#12

[eluser]Alfredor[/eluser]
Never mind I fixed it my selft.
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Ssh2
*
* Ssh2 library for Code Igniter.
*
* @package        Ssh2
* @author        Alfredo Rivera(http://niti2solutions.com)
* @version        1.0
*/

class Ssh2
{

   function __construct()
    {
        $this->ci =& get_instance();
    }
    
    /**
     * Connecto to server
     * @param    string
     * @return    string
     */    
   function connect($server,$port, $muser, $mpass)
    {
    if(!($con = ssh2_connect($server, $port))){
        echo "<font color='red'><b>Failure: Unable To Connect</b></font>";
    } else {
        if(!ssh2_auth_password($con, $muser, $mpass)) {
        echo "<font color='red'><b>Failure: Unable To Authenticate</b></font>";
        } else {
        echo "<font color='green'><b>Success: Logged In, Connected At ".$server." With User ".$muser." And Password ".$mpass."</b></font>";
            }
            }
    }
    
        /**
     * Send command to server
     * @param    string
     * @return    string
     */    
   function execute($server, $port,$muser,$mpass, $command)
   {
    $con = ssh2_connect($server, $port);
    ssh2_auth_password($con, $muser, $mpass);
    if(!($stream = ssh2_exec($con, $command))){
    echo "<font color='red'><b>Failure: Unable To Execute Command</b></font>";
    } else {
    echo "<font color='green'><b>Success: Command Sent</b></font>";
    }
   }  
  
       /**
     * Transfer a file from server
     * @param    string
     * @return    bool
     */
   function download($server,$port,$muser,$mpass,$rfile,$lfile)
   {
    if(!($con = ssh2_connect($server,$port))){
    echo "<font color='red'><b>Failure: Unable To Connect</b></font>";
    } else {
    if(!(ssh2_auth_password($con,$muser,$mpass))){
    echo "<font color='red'><b>Failure: Unable To Authenticate</b></font>";
    } else {}
    if(!(ssh2_scp_recv($con,$rfile,$lfile))){
    echo "<font color='red'><b>Failure: Unable To Download</b></font>";
    } else {
    echo "<font color='green'><b>Success: File Downloaded</b></font>";
      }
       }
   }
  
       /**
     * Creates a shell
     * @param    string
     * @return    bool
     * @return    string
     */
   function shell($server,$port,$muser,$mpass,$term = 'vt102',$env = NULL,$height = 80,$width = 30,$chars = SSH2_TERM_UNIT_CHARS)
   {
    if(!($con= ssh2_connect($server, $port))){
    echo "<font color='red'><b>Failure: Unable To Connect</b></font>";
    } else {
    if(!(ssh2_auth_password($con, $muser, $mpass))){
    echo "<font color='red'><b>Failure: Unable To Authenticate</b></font>";
    } else {}
    }
    if(!($stream = ssh2_shell($con, $term, $env, $height, $width, $chars))){
    echo "<font color='red'><b>Failure: Unable To Create Shell</b></font>";
    } else {
    echo "<font color='green'><b>Success: Shell Created</b></font>";
    }
   }
}
/* End of file ssh2.php */
/* Location: /application/libraries/ssh2.php */
#13

[eluser]attos[/eluser]
Nice.
The connect function in your library is not returning a connection. You can either remove it or modify it to return the $con.
#14

[eluser]Alfredor[/eluser]
Actually I was looking into that, I think I got to modify it somehow, anyways I was working on something else, but once I finish I'll fix it.




Theme © iAndrew 2016 - Forum software by © MyBB