简体   繁体   中英

ssh2_scp_send works only on windows, but doesn't copy on linux to amazon.com

I have a little script that copy files trough ssh2 library for php. I tried on different version of php on windows and it works as expected, but when i upload the code on Linux, it fails to copy the file.

Note that the php code is same, php version are same. I don't get any error messages. I use this command during presetting of file permissions. Before that i tried with fopen, fwrite trough sftp, but i failed to chmod the file after copy it.

Here is the code (if matters):

public function connect() {

    if (empty($this->ftpProps['ftpServer'])) {
        $result['errorMsg'] = 'No FTP Server Provided';
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }

    $this->ftpProps['ftpServer'] = str_replace("sftp://", "", $this->ftpProps['ftpServer']);
    $this->callFunc($this->callbackFunction, 'Connecting to: <b>'.$this->ftpProps['ftpServer'].'</b>...'."\n");

    if (!function_exists('ssh2_connect')) {
        $result['errorMsg'] = 'Missing Secure Shell2 Library';
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }

    $this->connection = ssh2_connect($this->ftpProps['ftpServer'], 22);
    if (empty($this->connection)) {
        $result['errorMsg'] = "Couldn't connect to ".$this->ftpProps['ftpServer'];
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }
    $this->callFunc($this->callbackFunction, '<span class="successfull">Done</span><br />');

    // login
    if (empty($this->ftpProps['ftpUser'])) {
        $result['errorMsg'] = 'No FTP UserName Provided';
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }

    $this->callFunc($this->callbackFunction, 'Logging in as <b>'.$this->ftpProps['ftpUser'].'</b>...'."\n");

    $this->authResult = ssh2_auth_password($this->connection, $this->ftpProps['ftpUser'], $this->ftpProps['ftpPass']);

    if (!$this->authResult) {
        $result['errorMsg'] = 'Failed to connect to '.$this->ftpProps['ftpServer'].'. Wrong credentials.';
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }

    $this->callFunc($this->callbackFunction, '<span class="successfull">Done</span><br />');
    $this->callFunc($this->callbackFunction, 'Initializing SFTP subsystem of <b>'.$this->ftpProps['ftpServer'].'</b>...'."\n");

    $sftp = ssh2_sftp($this->connection);
    if (!$sftp) {
        $result['errorMsg'] = 'Failed to initialize SFTP subsystem of '.$this->ftpProps['ftpServer'];
        $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
        return $result;
    }

    $this->callFunc($this->callbackFunction, '<span class="successfull">Done</span><br />');
    $result['message'] = 'Success';
    $result['isTestSuccessful'] = true;
    return $result;
}

//@Override
public function upload() {
    $timer = lloader()->getTimer();

    $uploadStartedOn = time();
    $connectResult = $this->connect();
    if (!empty($connectResult['errMsg'])) { return $connectResult; }

    $targetFileNames = $this->ftpProps['targetFileNames'];
    $sourceFileNames = $this->ftpProps['sourceFileNames'];

    foreach ($sourceFileNames as $nameIndex => $sourceFileName) {
        $targetFileName = $targetFileNames[$nameIndex];
        echoFlush('Uploading <b>'.$targetFileName.'</b>...');

        // use ssh2_scp_send instead fopen because of opportunity to set file mode. amazon disables chmod command
        $writeResult = ssh2_scp_send($this->connection, $sourceFileName, $this->ftpProps['destPath'].$targetFileName, 0755);
        if (!$writeResult) {
            $result['errorMsg'] = 'Failed to upload '.$targetFileName;
            $this->callFunc($this->callbackFunction, '<span class="error">'.$result['errorMsg'].'</span><br>'."\n");
            return $result;
        }
        $this->callFunc($this->callbackFunction, '<span class="successfull">Done</span><br />');
    }

    $this->callFunc($this->callbackFunction, '<span class="successfull">Done</span><br>'."\n");
    $this->callFunc($this->callbackFunction, 'The Upload Took '.$timer->getFormattedElapsedTime(time() - $uploadStartedOn).'<br>'."\n");

    $result['message'] = 'Success';
    $result['isSubmitSuccessful'] = true;
    return $result;
}

Here is the Linux distribution:

cat /etc/*-release
CentOS release 5.9 (Final)

Some of the returned lines of php -i command:

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, data,
               http, ftp, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
...
ssh2
libssh2 version => 1.2.9
banner => SSH-2.0-libssh2_1.2.9
...
Stream Wrapper support => compress.zlib://
zlib.output_compression => Off => Off
zlib.output_compression_level => -1 => -1

EDIT:

I just tried to use fopen/fwrite/fclose instead ssh_scp_send and got same results. I'm totaly confused. Here is the code for file upload:

$targetHandler = fopen("ssh2.sftp://".$this->sftp."/".$this->ftpProps['ftpDestPath'].$targetFileName, 'w');
        if (empty($targetHandler)) {
            $result['errorMsg'] = 'Failed to open '.$targetFileName;
            return $result;
        }
        $sourceHandler = @fopen($sourceFileName, "r");
        while (!feof($sourceHandler)) {
            $readBlock = fread($sourceHandler, 16384);
            $resultWrite = fwrite($targetHandler, $readBlock);
            if ($resultWrite === false) {
                $result['errorMsg'] = 'Failed to upload '.$targetFileName;
                fclose($targetHandler);
                fclose($sourceHandler);
                return $result;
            }
        }

        $chmodResult = ssh2_sftp_chmod($this->sftp, $this->ftpProps['destPath'].$targetFileName, 0755);
        if (empty($chmodResult)) {
            $result['errorMsg'] = 'Failed to chmod '.$targetFileName;
            return $result;
        }

Thanks.

EDIT 2: It seems this is amazon sftp specific issue. I tried this on other server and it works on both platform. First thing in my mind is some kind of protection by ip. What do you think? The amazon sftp server is located at: productads.amazon-digital-ftp.com

Looks like there are two pre-requisites for the SSH2 library, have you verified the Linux box meets them?

http://www.php.net/manual/en/ssh2.requirements.php

I found the reason for this behavior of the script. It is the ssh2 library version. On my localhost and one linux server it is SSH-2.0-libssh2_1.4.3. On the problematic machine the version of the library is SSH-2.0-libssh2_1.2.9. So it seems this is a library bug.

I leave this post for those who use this library and don't want to change their code.

Thanks for your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM