php sftp seg fault - php

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

Related

"Segmentation fault" when using phpseclib

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.

Couchbase Client for php not work

I download the source code from https://github.com/couchbase/php-ext-couchbase. I then I compile it in ubuntu use the following command .
$ phpize
$ ./configure
$ make
$ make test.
Then I got a couchbase.so in module , then I include the couchbase.so in php.ini. Then, restart the Apache. However, where I check the server with phpinfo(), I cannot find any couchbase module is installed.
Then when I run
$myCluster = new CouchbaseCluster('couchbase://couchbase:8091');
$cb = new Couchbase("couchbase:8091", "testbucket",
"testpassword", "test_bosh_bucket");
In apache log , I found that the error message is CouchbaseCluster Class is not found. And Couchbase class is not found. It seems PHP does not load the couchbase.so. Since I cannot find the couchbase in phpinfo() and the Class is not found. Anything that I am missed?
Please help. Thank,s
Install http://docs.couchbase.com/developer/c-2.4/download-install.html
I'm use:
<?php
$cluster = new CouchbaseCluster("192.168.0.90:8091", "Administrator", "password", "default");
$db = $cluster->openBucket('default');
for ($i = 1; $i <= 400; $i++) {
$DocumentName = "document_name_" . substr(md5(rand()), 0, 6);
$res = $db->insert($DocumentName, array("user_id" => md5(rand())));
var_dump($res);
}
?>
You need to use DLLs that match your PHP version. Please refer to below link. Use the Thread Safe version.
http://pecl.php.net/package/couchbase/1.1.5/windows
Then, use the following code.
$cb = new Couchbase("127.0.0.1:8091", "", "", "beer-sample");
$cb->set("foo", "bar");
var_dump("foo");
Note: Don't use accounts with admin permissions to perform data transactions. Either pass NULL as the username or name of the bucket.

How to connect to Linux using PHP?

I am working on a game-server hosting panel and I've been tried this:
$ssh = ssh2_connect('xx.xx.xx.xx', 22);
if(ssh2_auth_password($ssh, 'user', 'userpass'))
{
echo 'Success';
}
else
{
die('Fail');
}
But its just doesn't show any message (Success/Fail).
How can I make this connection work? ><
you probably do not have the libssh2 extension installed. it is a pretty big pain to install it so i will recommend you use this tool:
https://github.com/phpseclib/libssh2-compatibility-layer
include it at top of file and all your libssh2 function calls should work even if libssh2 is not installed.

ssh access to ec2 from php

From local ubuntu linux machine connecting ec2 machine by ssh access.
when i run this php script from terminal it executes fine and write the tailed entries in file. when i run from browser got this error in apache error log
ssh: Could not resolve hostname proxy2: Name or service not known
Found its due to apache user permission problem. whether my guess is right or wrong am not sure. Any one helps me to fix this issue.
php code:-
<?php
$ss = 'ssh proxy2 '.'tail -n 3 /out/speed_log.txt.1'.' > proxy2temp1';
system($ss);
?>
**Finally found an solution using phpseclib and solved my problem. am recommend phpseclib to connect amazon ec2 machines from php uing .pem file to help others share my sample code.
make sure .pem file needs permission to read**
sample code:
include('Net/SSH2.php');
include('Crypt/RSA.php');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('/pathtokey.pem'));
$ssh = new Net_SSH2('ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('user', $key)) {
exit('Login Failed');
}
echo $ssh->exec('tail -n 3 /out/_log.txt.1');

SCP with PHP - Does not work

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.

Categories