I tried ro execute SSH2 using PHP 5.6, but i got an error
No compatible server to client encryption algorithms found in Net/SSH2.php
It happened when i upgrade PHP 5.4 to 5.6, can anyone tell me how to solve it ?
here's my source
set_include_path(__DIR__ . '/' . PATH_SEPARATOR . get_include_path());
include('Net/SSH2.php');
$ssh = new Net_SSH2('10.48.51.118');
if (!$ssh->login('nfc', '#fb204')) {
exit('Login Failed');
}
error No compatible server to client encryption algorithms found in Net/SSH2.php
Check out the discussion thread here https://github.com/phpseclib/phpseclib/issues/434#issuecomment-50597527 it might help you to solve your issue.
You need to set include path for your SSH file
Related
I have this php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include_once("constants.php");
include('Net/SFTP.php');
$sftp = new Net_SFTP(Constant::QUICKCHECK_SERVER);
if (!$sftp->login(Constant::QUICKCHECK_USERNAME, Constant::QUICKCHECK_PASSWORD)) {
exit('Login Failed');
}
And when I run it I get this error :
Warning: include(Net/SFTP.php): failed to open stream
The problem is that I don't know how to properly install phpseclib.
It'd depend on where you installed it to. If you installed phpseclib 1.0 to includes then you'd need to do include('includes/Net/SFTP.php');. You'd also probably need to do set_include_path(get_include_path() . PATH_SEPARATOR . 'includes'); as well.
That said, if you install phpseclib with composer then all of this becomes a non issue. That'd be my recommendation.
I downloaded phpseclib-0.3.10 from http://phpseclib.sourceforge.net/
My php Version : PHP 5.2.4
OS : CentOS release 6.6
When I run following I am getting "Segmentation fault" at this line $ssh->login('username', 'password')
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
$ssh=new Net_SSH2('servername');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
?>
I am unable find the reason for this issue. Could you please help me to find the reason for this.
My guess: either the fsockopen() call or the stream_select() call. You can figure it out by putting die()'s in your code at successive points within login() and the functions called therein.
Looking it up... login() calls _login():
https://github.com/phpseclib/phpseclib/blob/0.3.10/phpseclib/Net/SSH2.php#L1801
_login() calls _connect():
https://github.com/phpseclib/phpseclib/blob/0.3.10/phpseclib/Net/SSH2.php#L1817
eg. before line 964 in Net/SSH2.php add die('this far'). If it says "this far" then try adding it after. If you get a seg fault when it's after but not before that probably means it's the fsockopen that's to blame.
And just keep on doing that until you get the seg fault. Do a die('this far'); before and after the stream_select and just where-ever. And then post the line you're getting it on.
I'm using php phpseclib.And i get following error when execute the script
PHP Fatal error: Class 'Crypt_RSA' not found
in /home/xxxxx/public_html/index.php on line 5
PHP SCRIPT
<?php
include('library/php/Net/SSH2.php');
$key = new Crypt_RSA();
$key->setPassword('891600909v');
$key->loadKey(file_get_contents('891600909'));<--This is the pvt key file in home directory in my ubuntu PC-->
$ssh = new Net_SSH2('www.xxxxx.com');
if (!$ssh->login('xxxxx.com', $key)) {
exit('Login Failed');
}
echo $ssh->read('xxxxx.com#xxxxx.com:~$');
$ssh->write("ls -la\n");
echo $ssh->read('xxxxx.com#xxxxx.com:~$');
?>
How could i solve this issue ?
You need to also include the RSA.php file for the Crypt_RSA class.
Add this underneath your other include (assuming you have this file downloaded):
include('library/php/Crypt/RSA.php');
Adding to what Ryan Kempt said make sure your include_path is set appropriately. eg.
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
You can either install phpseclib with pear OR get them from the site
http://phpseclib.sourceforge.net/pear.htm
Manually download each of the classes you wanted and include them in your main project.
In the current versions of phpseclib, the class Crypt_RSA is not existing anymore.
You create it this way:
$rsa = \phpseclib3\Crypt\RSA::load("...");
or
$rsa = \phpseclib3\Crypt\RSA::createKey();
Trying to upload a file to a different server using PHP. It connects with ssh_connect() successfully, but when I include ssh2_scp_send the page that is displayed is blank (as though there is a syntax error), and it certainly did not upload the file. Any idea why this may be? I'd prefer to use PHP, but if it doesn't work I suppose I could make a system call to CLI scp with private key files, but I'd prefer not to do that. Alternative suggestions are welcomed.
Info: debian w/apache
Installed: libssh2-1-dev libssh2-php php-pear php5-dev; pecl install -f ssh2
$ssh = ssh2_connect('localhost', 22);
ssh2_auth_password($ssh, 'user', 'password');
ssh2_scp_send($ssh, 'original', 'destination'); # does not work
ssh2_exec($ssh, 'exit'); #does not help
Any ideas?
Personally, I'd recommend using phpseclib, a pure PHP SFTP implementation:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Chief among libssh2's problems is the fact that it's not easy to install. The original authors have abandoned it. The source code on php.net requires modification for it to even compile and although you can install it with apt-get on Ubuntu right now what happens when it requires more than just two new lines to work? And what if the Linux box you're trying to get it working on isn't Ubuntu?
If you ever need to create a new server on which to host your website you don't want to be stuck spending hours on a bunch of esoteric dependencies.
What i need to do if next code gives me seg fault ?
$handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
$file = readdir($handle);
closedir($handle);
where $sftp is
$this->_connection = ssh2_connect($this->_server, 22);
if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
} else {
ssh2_auth_password($this->_connection,$this->_user,$this->_password);
}
$sftp = ssh2_sftp($this->_connection);
Connect work well. and segfault is when only i use readdir function.
I'm answering this old question due to high ranking on Google search. I was experiencing the same segmentation fault and the real solution wasn't here.
It turns out that since PHP 5.6, the behavior of the function ssh2_sftp changed and is now returning a resource that doesn't allow to be concatenated as a string to form filesystem paths.
So, once you have the return of the ssh2_sftp, you have first to pass it through intval in order to build the remote file path:
$sftp = ssh2_sftp($this->_connection);
$handle = opendir('ssh2.sftp://' . intval($sftp) . '/usr/bin/');
$file = readdir($handle);
closedir($handle);
You are welcome.
I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3
sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3
It worked for me after some problems.
Regards
A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. You should file a bug against the extension in question.
Don't forget to include a short, reproducible example. Since this may be only reproducible with your specific combination of ssh client and server, you should reproduce the bug first in a brand-new VM and record every step you make, like this:
Install Ubuntu 11.04 amd64 in the VM
Install ssh with $ sudo apt-get install ssh
Configure ssh with ...
Install php with $ sudo apt-get install php5-cli
Copy the script [link to http://pastebin.com/ here] to the VM
Type php ssh-segfault.php and receive a segfault.
Not applicable to the OP's situation, but if you fail to call closedir explicitly on the handle created by opendir, you'll also get a segfault during PHP internal cleanup at script end.
I had a similar problem, though with some additional factors. I'll start with the solution: use absolute paths, rather than relative. And avoid the ~.
As for my findings. Take this script:
#!/usr/bin/php
<?php
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
unset($res); unset($ssh);
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
Results in an authentication error in the 2nd ssh2_auth_pubkey_file call. A bit strange to encounter, after two identical calls - but easy enough to analyze.
But having the rest of my application code around it would result in the segfault. Eventually I narrowed it down to that that including a file, that defines a function that uses the __FILE__ constant changes the error to turn into a segmentation fault.
Even a file as simple as this:
<?php
function nothing() {
error_log(__FILE__);
}
I hope this helps somebody else at some point...
One of the answers above suggests you need to cast the resource to an int intval($sftp) to avoid the segfault.
While I believe this used to be the case, this now appears to have inverted, at least on php 7.1.9 and casting to an int now causes the segfault, at least with file_put_contents:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . intval($sftp) . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://2675/var/file.text
Segmentation Fault
Without a cast it works:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . $sftp . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://Resource id #2675/var/file.text