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.
Related
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
I want to connect to another server by ssh using PHP but i get error as below .i looked for it very much but couldn't find any suitable solution for me.
Warning: ssh2_connect(): Error starting up SSH connection(-1): Failed sending banner in ...
Warning: ssh2_connect(): Unable to connect to ...
this is my php code :
$connection = ssh2_connect($details[0]['address'], $details[0]['port']);
ssh2_auth_password($connection, $details[0]['username'], $details[0]['password']);
Trying to diagnose issues with libssh2 is nigh impossible. My recommendation: use phpseclib, a pure PHP SSH implementation, and assuming that doesn't resolve the issue you can get logs that'll assist in the diagnosis. eg.
<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
echo $ssh->getLog();
?>
I need to use ssh on php. I eanble these two dlls in php.ini file:
extension=php_ssh2.dll
extension=php_ssh2.dll
I am still getting this error, any ideas what I might be missing?'
Fatal error: Call to undefined function ssh2_connect()
You probably need the libssh2 DLL's too.
I think phpseclib, a pure PHP SSH2 implementation, would work better for you actually. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
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
I'm having trouble using PHP to SFTP upload files to a remote server. When I use cURL, I'm getting the error described here:
SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?
I also tried phpseclib as suggested in:
SFTP from within PHP
But when i try phpseclib, i get these errors:
Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53
Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53
I then tried using system commands in php like so, but nothing happened:
<?php
echo system('sftp user#ftp.domain.com');
echo system('password');
echo system('put file.csv');
?>
I also tried
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>
but my php server says ss2_connect is not defined.
I tried to do the following from terminal
scp file.csv user#ftp.remote.com
password
But the server does not allow scp command. I do not have shell access to create ssh keys.
All i can do right now is sftp from terminal and manually upload. But I really want to automate this so a PHP website can do all this.
There aren't many tutorials on how to SFTP upload from PHP. Is it because it's a bad thing to do? If so, what should I be doing? The server I want to upload to only allows sftp connections.
http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct
Per that, phpseclib's root directory needs to be in your include_path. From that page:
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SFTP.php');
?>
You should really familiarize yourself with this kind of include technique - it's pretty standard for PEAR libraries and Zend ones.
but my php server says ss2_connect is not defined.
For this error, you should install ssh2 extension on your server's php.
Use the built in SSH library. Using the scp function will be easier than initiating a sftp session too.
http://www.php.net/manual/en/function.ssh2-scp-send.php
To use system command you have to:
Configure password-less ssh
Run command like scp filename1 userid#hostname:filename2, check man scp. You can do the same with sftp but with some other options
You can use Pure-PHP implementation of SSHv2 library.
Here are some examples of how to use this library:
<?php
include 'vendor/autoload.php';
$ssh = new \phpseclib\Net\SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
And:
<?php
include 'vendor/autoload.php';
$key = new \phpseclib\Crypt\RSA();
//$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
$ssh = new \phpseclib\Net\SSH2('www.domain.tld');
if (!$ssh->login('username', $key)) {
exit('Login Failed');
}
echo $ssh->read('username#username:~$');
$ssh->write("ls -la\n");
?>