I'm new to PHP and the phpseclib implementation of SSH.
I have the following code :
$ssh = new Net_SSH2($_SESSION['targetAddress']);
if (!$ssh->login(SSH_USER, SSH_PASSWORD)) {
exit('Login Failed');
}
$ssh->setTimeout(400);
$a = 0;
while(isset($file[$a])) {
$ssh->exec('cd '.$_SESSION['path'].'; find -L '.$file[$a].' > /tmp/ligacoes; for i in `cat /tmp/ligacoes`; do cp $i /var/tmp/; done');
$a++;
}
What I am trying to accomplish here is to copy files chosen by user on a remote server to a new directory on the same server. When executing the script, it successfully find and copy the first file to the new directory, but after that the script just stops. Even if the user choose just one item the script hangs and does not continue. It doesn't even increment $a
Any thoughts on what may be happening ?
UPDATE:
Real Time NET_SSH2 Log
I also ran the command directly in the server and it works perfectly. I guess the issue is limited to $ssh->exec();
UPDATE 2:
I changed my $ssh->exec('cd '.$_SESSION['path'].'; find -L '.$file[$a].' > /tmp/ligacoes; for i in 'cat /tmp/ligacoes'; do cp $i /var/tmp/; done'); to $ssh->exec('cd '.$_SESSION['path'].'; cp '.$file[$a].' /var/tmp;'); and that solved part of the problem. Now I am able to copy one selected file to a new directory and the script does not hang. The issue keeps happening when two or more files are selected.
Things that may help:
$ssh->exec echos both stdout and stderr. Check those. echo $ssh->exec('echo hello');exec() to the bash file for processing. Something like:mybash.sh
cd /example/path/;
find -L example_file > /tmp/ligacoes;
for i in `cat /tmp/ligacoes`;
do cp $i /var/tmp/;
done
and in your php
$ssh->exec('mybash.sh');
If that works, then you can expand it to send variables
cd $1;
find -L $2 > /tmp/ligacoes;
for i in `cat /tmp/ligacoes`;
do cp $i /var/tmp/;
done
calling it like this where $_SESSION['path'] will be $1:
$ssh->exec('mybash.sh '.$_SESSION['path'].' '.$file[$a]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With