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");
?>
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 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();
?>
Are there any possibilities to connect to server via ssh using php in tidesdk.
I'm trying to use
$session = ssh2_connect($server, 22);
But an error occurred
Fatal error - Call to undefined function ssh2_connect()
I'd recommend using phpseclib, a pure PHP SSH implementation. 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');
?>
The fact that it's pure-PHP means it's ultra-portable. Unlike the libssh2 extension you're currently trying to use.
Have you installed the SSH2 extension?
http://ie1.php.net/manual/en/ssh2.installation.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');
?>
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');
?>