Skip to content
Advertisement

Problem to download binary file using phpseclib SCP

I have a a small PHP script that uses phpseclib to download files from remote server.

The script is like below:

    include('Net/SCP.php');
    echo var_dump($ssh->exec('whoami')); // debug to test the ssh connection. returns "myuser"
    $scp = new Net_SCP($ssh);
    try{
        $remotePath = '/home/user/test.txt';
        $localPath = '/tmp/myfile';
        if (!$scp->get($remotePath, $localPath)) {
            throw new Exception("Problems to get file");
        }
    } catch (Exception $e) {
        echo "nn" . var_dump($e->getMessage()) . "nn";die;
    }

There are some other questions here in SO that uses very similar snippets. It works like a charm for many files, but it fails for some binary files ($remotePath = '/home/user/test.p12';, for instance).

Are there any know limitation to download binary files using phpseclib (I didn’t find anything in their issues on github)? If not, what I’m doing wrong? Am I forgetting some options or anything?

As a side note scp myuser@serverip:/home/user/test.p12 /tmp/teste.p12 works fine in command line.

Following the comments, I must indicate that my script just fails. The statment $scp->get($remotePath, $localPath returns false for all binary files that I tried. Thats all i have for now. As far as I know, phpseclib does not have any detailed log on these fails.

My application log (nginx) does not show anything special. Access log on my remote server (centOS. for these tests I have the control over it, but its not the real scenario) I got something like below:

Jul 27 15:22:58 localhost sshd[14101]: Accepted password for myuser from myip port 51740 ssh2
Jul 27 15:22:58 localhost sshd[14101]: pam_unix(sshd:session): session opened for user myuser by (uid=0)
Jul 27 15:22:58 localhost sshd[14103]: Received disconnect from myip port 51740:11:
Jul 27 15:22:58 localhost sshd[14103]: Disconnected from user myuser myip port 51740

PHP version: 7.3 (the script is also used in servers with older versions)

Local server: Debian 10

Remote server: CentOs 8

The problematic file that fires the problem is a certificate p12 file.

Advertisement

Answer

I found the problem and it was much more simple than I tought. It was just a permission problem (for any good reason, I put all my test files in a directory without read permition).

I decided to leave this answer here, because I think that this is not clear on phpseclib documentation, but the ‘Net/SCP.php’ only works with files with read permission, so, before download make sure that the file are readable or execute something like chmod o+r filename.

The snippet in the question works fine with binary files.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement