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