phpseclib - issues with php versions - php

Running on PHP 5.2.13 this works fine :
each line of output is shown..
include('includes/Net/SSH2.php');
$ssh = new Net_SSH2('127.0.0.1');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}
function packet_handler($str)
{
echo $str;
}
$ssh->exec('ping 127.0.0.1', 'packet_handler');
Running the same code on PHP 5.3.17 returns nothing.
I'd have to use something like
echo $ssh->exec('timeout 5 ping 127.0.0.1');
And wait for it to time out.
Any idea why this isn't working on the new version ?
Thanks

Try use new NET_SSH2 version. I used composer and your code work well for me (I have PHP 5.6 on my local PC). My steps was:
1) I created new empty folder
2) Run composer:
composer require "phpseclib/phpseclib"
this will create vendor folder with NET_SSH2 version = 0.3.9
3) Then I changed your source code (for composer autoload):
require 'vendor/autoload.php';
$ssh = new Net_SSH2('127.0.0.1');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}
function packet_handler($str) {
echo $str;
}
$ssh->exec('ping 127.0.0.1', 'packet_handler');
And all work well. I think you have the problem because you are using old library version. You can try use composer or just download new library version from GitHub.
I hope it will help.

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

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.

PHP Fatal error: Class 'Crypt_RSA' not found

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();

how do you make an ssh connection within php

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

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