Jump to content

PHP SSH2 issues.


ChrisMartino

Recommended Posts

Hey there!

 

Thanks for taking the time to read my thread. So basically I have a basic SSH2 class to handle all my command requests. Yet when I run two commands simultaneously using the script like so:

 

$c_method->exec("killall -9 -u local_user");

$c_method->exec("cd /home/local_user && nohup ./process &"); 

 

I am prompted with the following error:

 

Warning: ssh2_exec() [function.ssh2-exec]: Unable to request a channel from remote host in ssh2.class.php on line 58

 

 

Here is my command processing function:

 

public static function exec($command_string, $stream = false)
{
$stream_exec = ssh2_exec(self::$global_ssh2_handle, $command_string);

if($stream != false) 
{
	stream_set_blocking($stream_exec, true);

	return stream_get_contents($stream_exec);
}
}

 

Thanks for your time and I hope you can help!

Link to comment
https://forums.phpfreaks.com/topic/241626-php-ssh2-issues/
Share on other sites

that class according to what i read is outdated and buggy why not use something like this....

 

<?php
/*
SSH2 PHP5 OOP Class
Copyright (C) 2011 - Jine (http://jine.se)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*    @desc Simple PHP5 Class for usage with libssh2-php (ssh2)
*    @author Jim Nelin & Jine - http://jine.se
*    @date 2011-02-09
*
*    Example;
*    ----------------------------------------------
*    require_once("SSH2.php");
*
*    $ssh = new SSH2("hostname.com");
*    
*    // With auth with password:
*    $ssh->auth("root", "xxx");
*    
*    // Or public key:
*    $ssh->auth("root", "~/.ssh/id_rsa.pub", "~/.ssh/id_rsa", "keypassword");
*    
*    $ssh->exec("id");
*    echo $ssh->output();
*    ----------------------------------------------
*    
**/

class SSH2 {

    var $ssh;
    var $stream;
        
    function __construct($host, $port=22) {
        if (!$this->ssh = ssh2_connect($host, $port)) {
            return false; 
        }
    }

    function auth($username, $auth, $private = null, $secret = null) {
        if(is_file($auth) && is_readable($auth) && isset($private)) {
            // If $auth is a file, and $private is set, try pubkey auth
            if(!ssh2_auth_pubkey_file($this->ssh, $username, $auth, $private, $secret)) {
                return false;
            }
            
        } else {
        
            // If not pubkey auth, auth with password
            if(!ssh2_auth_password($this->ssh, $username, $auth)) {
                return false;
            }
            
        }
        
        return true;
    }

    function send($local, $remote, $perm) {
        if(!ssh2_scp_send($this->ssh, $local, $remote, $perm)) { 
            return false; 
        }
            
        return true;
    }

    function get($remote, $local) {
        if(ssh2_scp_recv($this->ssh, $remote, $local)) {
            return false;
        } 
    
        return true;
    }

    function cmd($cmd, $blocking = true) {
        $this->stream = ssh2_exec($this->ssh, $cmd);
        stream_set_blocking($this->stream, $blocking);
    }
    
    // Just an aliasfunction for $this->cmd
    function exec($cmd, $blocking = true) {
        $this->cmd($cmd, $blocking = true);
    }

    function output() {               
        return stream_get_contents($this->stream);
    }

} 

Link to comment
https://forums.phpfreaks.com/topic/241626-php-ssh2-issues/#findComment-1241060
Share on other sites

  Quote

that class according to what i read is outdated and buggy why not use something like this....

 

<?php
/*
SSH2 PHP5 OOP Class
Copyright (C) 2011 - Jine (http://jine.se)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*    @desc Simple PHP5 Class for usage with libssh2-php (ssh2)
*    @author Jim Nelin & Jine - http://jine.se
*    @date 2011-02-09
*
*    Example;
*    ----------------------------------------------
*    require_once("SSH2.php");
*
*    $ssh = new SSH2("hostname.com");
*    
*    // With auth with password:
*    $ssh->auth("root", "xxx");
*    
*    // Or public key:
*    $ssh->auth("root", "~/.ssh/id_rsa.pub", "~/.ssh/id_rsa", "keypassword");
*    
*    $ssh->exec("id");
*    echo $ssh->output();
*    ----------------------------------------------
*    
**/

class SSH2 {

    var $ssh;
    var $stream;
        
    function __construct($host, $port=22) {
        if (!$this->ssh = ssh2_connect($host, $port)) {
            return false; 
        }
    }

    function auth($username, $auth, $private = null, $secret = null) {
        if(is_file($auth) && is_readable($auth) && isset($private)) {
            // If $auth is a file, and $private is set, try pubkey auth
            if(!ssh2_auth_pubkey_file($this->ssh, $username, $auth, $private, $secret)) {
                return false;
            }
            
        } else {
        
            // If not pubkey auth, auth with password
            if(!ssh2_auth_password($this->ssh, $username, $auth)) {
                return false;
            }
            
        }
        
        return true;
    }

    function send($local, $remote, $perm) {
        if(!ssh2_scp_send($this->ssh, $local, $remote, $perm)) { 
            return false; 
        }
            
        return true;
    }

    function get($remote, $local) {
        if(ssh2_scp_recv($this->ssh, $remote, $local)) {
            return false;
        } 
    
        return true;
    }

    function cmd($cmd, $blocking = true) {
        $this->stream = ssh2_exec($this->ssh, $cmd);
        stream_set_blocking($this->stream, $blocking);
    }
    
    // Just an aliasfunction for $this->cmd
    function exec($cmd, $blocking = true) {
        $this->cmd($cmd, $blocking = true);
    }

    function output() {               
        return stream_get_contents($this->stream);
    }

} 

 

I would rather not. But funnily enough when I run them commands as root I'm not prompted with any sort of error but running them as another Linux user I am?

Link to comment
https://forums.phpfreaks.com/topic/241626-php-ssh2-issues/#findComment-1241643
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.