How to view directory files on another server using ssh2

I would like to list the directory files on another server

I am connected to another server using the ssh2_connect function, the connection is going well and I can get the file I need, but I'm not sure how the files can be listed.

Any help is determined!

+9
source share
3 answers

You can use ssh2_sftp and opendir , for example:

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);
$sftp_fd = intval($sftp);

$handle = opendir("ssh2.sftp://$sftp_fd/path/to/directory");
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false != ($entry = readdir($handle))){
    echo "$entry\n";
}
+30
source

, - , PHP 5.6.28, , , , intval() SFTP-/

$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");
+17

http://www.php.net/manual/en/function.ssh2-exec.php

You give it a command ls, assuming it is a UNIX-based system (usually this is the case), otherwise an OP command, such as dirfor Windows.

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection, 'ls');
?>
+1
source

Source: https://habr.com/ru/post/1663605/


All Articles