2

I want to change directory in PHP with phpseclib library but it's doesn't works.

$ssh = new Net_SSH2('localhost');
if (!$ssh->login('root', 'pass')) {
    exit('Failed to connect!');
}
$ssh->exec('cd /home/');
echo $ssh->exec('ls');

Why this is always output only root directory files/folders... and don't change the directory to home?

Nathan J
  • 89
  • 7

1 Answers1

2

If you read the documentation, it states:

If done on an interactive shell, the output you'd receive for the first pwd would (depending on how your system is setup) be different than the output of the second pwd. The above code snippet, however, will yield two identical lines.

The reason for this is that any "state changes" you make to the one-time shell are gone once the exec() has been ran and the channel has been deleted.

You can workaround this on Linux by doing $ssh->exec('cd /; pwd')

So you need to instead put both commands in the same exec:

$ssh->exec('cd /home/; ls');
Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133