0

A data supplier have requested a public SSH key for using their SFTP server. They have supplied us with:

  • Username
  • Password
  • Hostname
  • Port

I am attempting to use the stream wrapper ssh2.sftp// within php using private key and username/password. My code is as follows:

$options = array(
    'ssh2' => array(
        'privkey_file' => '/path/to/priv_key',
    ),
);
$context = stream_context_create($options);
fopen('ssh2.sftp://<username>:<password>@<host>:<port>', 'rb', false, $context);

Results:

Warning: fopen(ssh2.sftp://...@<ip>...): failed to open stream: operation failed in ...

I can connect using linux's sftp command, so I know that the connection works and the credentials are working just fine.

sftp -oPort=<port> -i /path/to/priv_key -v <username>@<host>

Results:

Authenticated with partial success.
password: <password-entered>
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Sending subsystem: sftp
sftp>

Does anyone have any ideas?

I am open to ideas using the ssh2 functions, but I am strongly wanting to use the file system and stream functions.

HenryHayes
  • 368
  • 2
  • 4
  • 13
  • Password + key worked fine for me using cURL. Someone else has posted a [cURL stream wrapper](http://stackoverflow.com/questions/1342583/manipulate-a-string-that-is-30-million-characters-long/1342760#1342760). – Padrig Sep 02 '13 at 15:40
  • I think [supplied resource is not a valid SSH2 SFTP resource](http://stackoverflow.com/a/20972014/1177774) is the answer for this question. – jibran Jul 11 '16 at 06:45

1 Answers1

0

Honestly, I think you'd be better off using phpseclib, a pure PHP SFTP implementation. Among other things it's easier to diagnose issues with that than it is with libssh2.

Also, if you want to use file system and stream functions phpseclib supports those as well:

https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SFTP/Stream.php

neubert
  • 15,947
  • 24
  • 120
  • 212