0

My script generates a file that I need php to send via sftp to an ipv6 address.

My first attempts are inconclusive using ssh2_connect() and ssh2_auth_password (). Basically it seems I do not provide a valid $host value.

$host = "[XXXX:XXXX:XX:...]"; // ipv6 enclosed in []
$port = 22;
$user = "XXX";
$pass = "XXXXXXX";

$connection = ssh2_connect($host, $port);

if (ssh2_auth_password($connection, $user, $pass)) {
  error_log("valid connection");
} else {
  error_log("failed connection");
}
  • the error log returns:

PHP Warning: ssh2_connect(): php_network_getaddresses: gethostbyname failed

PHP Warning: ssh2_connect(): Unable to connect to [XXXX:XXXX:XX:...] on port 22...

Is there another way to go about this ?

Thank you

Flo H
  • 49
  • 6
  • The code you've provided doesn't seem to have any relevance to the question. If you've tried ssh2_sftp(), show us that code, and tell us what happened - did you get an error? did the file not get uploaded to the remote host? – IMSoP Sep 29 '21 at 17:04
  • Thank you for your feedback, I focused my question and provided more relevant code – Flo H Oct 04 '21 at 13:14
  • I posted this question which was quickly closed so I modified it the next day, but since is has been 5 days and the question is still closed, I took the initiative to ask the question again... – Flo H Oct 05 '21 at 09:34

1 Answers1

0

I have not fully resolved this question, but if you come here with a similar need here are my two cents, what I learned in the process.

  • orient yourself to using curl.
  • you basically need the url not the ipv6 address, for the DNS.
  • you can sftp with curl to your desired location with authentication, even if it is a nested folder and authentication happens at root level.

for debug: it can be useful to check curl_getinfo() prior to an after curl_exec() as well as to echo the return transfer.

$user = 'XXXX';
$pass = 'XXXX';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "sftp://auth_level/nested_directory/nested_further");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

echo(var_dump(curl_getinfo($ch)));
$result = curl_exec($ch);
echo('<br>');
echo(var_dump(curl_getinfo($ch)));
echo "<pre>$result</pre>";

Here is a blog post on uploading a file with curl

here is a useful SO answer.

Good luck

Flo H
  • 49
  • 6