phpseclib fails more than succeeds - php script

54 Views Asked by At

Recently had to change from FTP to SFTP to upload some data files to 2 remote servers. Files are typically 1 - 4 Mb in size

I am getting errors ( fails to upload ) around 50% of the attempts. This is happening to both servers that I am uploading to ( so not 1 server only ). This is over multiple days of running the php script.

If I manually connect to the servers using Filezilla ( from my laptop, on same LAN as the Pi running the php script ), I can connect and upload without errors every time.

The fact that some uploads work, seems to indicate that the code does not contain any errors.

The Filezilla connection does seem a lot slower than a normal FTP connection, both for the login and the directory listing.

I am guessing that Filezilla has a better retry / timeout procedure than the phpseclib ?

Are there features / code in phpseclib that will make it more reliable under these conditions ?

Code and errors listed below ( IP and username changed ).

<?php

    $CodeRoot = dirname(__FILE__);
    set_include_path(get_include_path() . PATH_SEPARATOR . $CodeRoot.'/phpseclib');
    include($CodeRoot.'/phpseclib/Net/SFTP.php');

    $RetryCount = 0;
    $MaxRetryCount = 3;
    do {  // repeat loop for 3 times unless upload OK and file sizes match

        $RetryCount = $RetryCount + 1;
        echoline("----- Upload Attempt  # ".$RetryCount."  to: $ftpServer");
        /// note : the 'echoline' code simply writes the message to a date specific text/log file and mails it back to me at the end

            $ftp_conn11 = new NET_SFTP($ftpServer);
            if ($ftp_conn11){
                $ftp_login = $ftp_conn11->login($ftpUser, $ftpPassword);
                if (!$ftp_login) {
                    echoline("Login Failed");
                }
                if ($ftp_login){
                    if($ftp_conn11->put($remote_file, $local_file, NET_SFTP_LOCAL_FILE)){
                        echoline("----- File uploaded successfully to: $ftpServer");
                        $remoteSize = $ftp_conn11->size($remote_file);
                        $localSize = filesize($local_file);
                        if ($remoteSize == $localSize){
                            echoline("ALL OK ----- The FTP file size and LOCAL file sizes match");
                            $RetryCount = $MaxRetryCount + 2; // break out of the 3x retry loop
                        }
                        if ($remoteSize != $localSize){
                            echoline("ERROR ----- The FTP file size and LOCAL file size does not match");
                        }
                    } else {
                        $msg = "Cannot upload to ftp $ftpServer, Using user $ftpUser";
                        echoline($msg);
                    }
                }else{
                    $msg = "Cannot login to FTP server: $ftpServer using user $ftpUser";
                    echoline($msg);
                }
            }else{
                $msg = "Cannot connect to FTP server: $ftpServer";
                echoline($msg);
            }  // end of : if ($ftp_conn11)
            $ftp_conn11->disconnect();
        sleep(3);
    } while ($RetryCount <= $MaxRetryCount);
    echoline("---------------------------------------------------------------");
?>

/// note : the 'echoline' code simply writes the message to a text file and mails it back to me at the end

18:00:11 --- ----- Upload Attempt # 1 to: $TargetServer_1 PHP Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2587

Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2587 18:00:36 --- Cannot upload to ftp $TargetServer_1, Using user $userName 18:00:39 --- ---------------------------------------------------------------

18:00:39 --- ----- Upload Attempt # 2 to: $TargetServer_1 18:01:06 --- ----- File uploaded successfully to: $TargetServer_1 18:01:07 --- ALL OK ----- The FTP file size and LOCAL file sizes match 18:01:10 --- ---------------------------------------------------------------

19:20:22 --- ----- Upload Attempt # 1 to: $TargetServer_2 PHP Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553

Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553 19:20:52 --- Cannot upload to ftp $TargetServer_2, Using user $userName 19:20:55 --- ---------------------------------------------------------------

19:20:55 --- ----- Upload Attempt # 2 to: $TargetServer_2 PHP Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400

Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400 19:21:13 --- Cannot upload to ftp $TargetServer_2, Using user $userName 19:21:16 --- ---------------------------------------------------------------

19:21:16 --- ----- Upload Attempt # 3 to: $TargetServer_2 PHP Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553

Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553 19:21:43 --- Cannot upload to ftp $TargetServer_2, Using user $userName 19:21:46 --- ---------------------------------------------------------------

19:21:46 --- ----- Upload Attempt # 4 to: $TargetServer_2 Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400 19:22:04 --- Cannot upload to ftp $TargetServer_2, Using user $userName PHP Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400 19:22:07 --- ---------------------------------------------------------------

18:00:32 --- ----- Upload Attempt # 1 to: $TargetServer_2 18:00:50 --- Cannot upload to ftp $TargetServer_2, Using user $userName 18:00:53 --- ---------------------------------------------------------------

18:00:53 --- ----- Upload Attempt # 2 to: $TargetServer_2 18:01:03 --- Login Failed 18:01:03 --- Cannot login to FTP server: $TargetServer_2 using user $userName 18:01:07 --- ---------------------------------------------------------------

18:01:07 --- ----- Upload Attempt # 3 to: $TargetServer_2 18:01:23 --- Cannot upload to ftp $TargetServer_2, Using user $userName 18:01:26 --- ---------------------------------------------------------------

18:01:26 --- ----- Upload Attempt # 4 to: $TargetServer_2 18:01:43 --- Cannot upload to ftp $TargetServer_2, Using user $userName 18:01:46 --- ---------------------------------------------------------------

19:20:10 --- ----- Upload Attempt # 1 to: $TargetServer_1 PHP Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553

Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553 19:20:47 --- Cannot upload to ftp $TargetServer_1, Using user $userName 19:20:50 --- ---------------------------------------------------------------

19:20:50 --- ----- Upload Attempt # 2 to: $TargetServer_1 PHP Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400

Notice: Expected SSH_FXP_HANDLE or SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2400 19:21:06 --- Cannot upload to ftp $TargetServer_1, Using user $userName 19:21:09 --- ---------------------------------------------------------------

19:21:09 --- ----- Upload Attempt # 3 to: $TargetServer_1 PHP Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553

Notice: Expected SSH_FXP_STATUS in /config/www/folder1/phpseclib/Net/SFTP.php on line 2553 19:21:24 --- Cannot upload to ftp $TargetServer_1, Using user $userName 19:21:27 --- ---------------------------------------------------------------

19:21:27 --- ----- Upload Attempt # 4 to: $TargetServer_1 19:21:43 --- ----- File uploaded successfully to: $TargetServer_1 19:21:43 --- ALL OK ----- The FTP file size and LOCAL file sizes match 19:21:46 --- ---------------------------------------------------------------

0

There are 0 best solutions below