ssh2.so unable to load on CentOS 5 - php

I've been trying to install the SSH2 libraries for php onto a web server running CentOS 5 with PHP 5.1.6 and was able to successfully install all the dependencies, but after restarting the web server I get the following error:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/ssh2.so' - libssh2.so.1: cannot open shared object file: No such file or directory in Unknown on line 0
Has anyone run into this before? The ssh2.so file exists at '/usr/lib64/php/modules/ssh2.so' and has the same file permissions as all the other extensions (-rwxr-xr-x).
Thanks

The key to the error is this:
libssh2.so.1: cannot open shared object file: No such file or directory
Your ssh2.so file can't find it. Run this and you'll see what I mean:
ldd /usr/lib64/php/modules/ssh2.so
Try this:
updatedb
locate libssh2.so.1
If found, you may need to create a symlink inside /usr/lib64/ or something.
If that doesn't find it, a quick google search brings up this as a hit for libssh2 on centos5:
http://centos.karan.org/el5/extras/testing/x86_64/RPMS/libssh2-0.18-10.el5.kb.x86_64.rpm
Install that, restart apache, and try it again.

Maybe phpseclib, a pure PHP SSH implementation, would work better for you? An example:
<?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');
?>

Related

Why can't install php-ssh2 on my win7?

I do as some material say.
1. download php extension ssh2 from
http://windows.php.net/downloads/pecl/releases/ssh2/0.12/
i select php_ssh2-0.12-5.4-ts-vc9-x86.zip to download.
2. extract the downloaded file
there are thress files :libssh2.dll、php_ssh.dll、php_ssh2.pdb .
3. save php_ssh.dll and php_ssh2.pdb in the php/ext/.
4. save libssh2.dll in the c:/windows/system32 and c:/windows/syswow64 .
5. edit php.ini
to add a line: extension=php_ssh2.dll
6. reboot apache .
But there is no ssh2 info in my output of phpinfo().
Why can't install php-ssh2 on my win7?
Trying to use libssh2 is a PITA as you're finding out. I'd just use phpseclib, a pure PHP SSH implementation. Example:
<?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');
?>
It has a number of advantages over libssh2:
http://phpseclib.sourceforge.net/ssh/compare.html

phpredis errors Class Redis not found in Linux

I met a weied problem when installing phpredis by
cd phpredis && ./configure && make && make install
after that, I add
extension=redis.so
into php.ini.
I can get an OK by running
php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"
BUT when running http:127.0.0.1, nginx throw a error " Fatal error: Class 'Redis' not found in index.php"
<?php>
$client = new Redis();
<?>
I guess this may be some problems related with environment...
Thanks for any advice!
The command line probably does not use the same php.ini file than the web server.
Use phpinfo(); to know which configuration file is loaded in both cases and then declare your extension in the ini file used by your web server.
I had this issue minutes ago, and I solved it restarting the server, this way the server refresh *.ini files
If you're using composer and get the error "Class Redis not found" try put a backslash before the name class. Like this:
<?php
$client = new \Redis();
<?

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.

How to SFTP upload files from PHP

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");
?>

Categories