F
F
Fyodor2016-11-22 15:00:34
PHP
Fyodor, 2016-11-22 15:00:34

How to work with sftp from php with authorization using a private SSH-2 RSA key?

I can’t figure it out, basically everything I find points to
https://github.com/phpseclib/phpseclib
however it has some insane documentation, examples from which do not work (errors, or they are from a different version).
I'm trying to do so

<?php

include 'vendor/autoload.php';

$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . 'vendor/phpseclib');
$loader->register();

use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;
use phpseclib\Net\SFTP;

$key = new RSA();
$key->setPassword('***');
$key->load(file_get_contents('****.ppk'));

// Domain can be an IP too
$ssh = new SFTP('***',22);
if (!$ssh->login('***', $key)) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');

I get an error
This service allows sftp connections only. This service allows sftp connections only.

what's wrong?
via FTP client works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vamp, 2016-11-22
@Richard_Ferlow

The exec() method is an SSH2 feature. Since the server you are connecting to does not provide you with full SSH access, but only SFTP, you must also call the methods from the SFTP class:
$ssh->chdir()/nlist()/stat()/get() /put()/delete() and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question